Displaying a Number in Indian Format Using JavaScript

Displaying a number in Indian format using Javascript

For Integers:

    var x=12345678;    x=x.toString();    var lastThree = x.substring(x.length-3);    var otherNumbers = x.substring(0,x.length-3);    if(otherNumbers != '')        lastThree = ',' + lastThree;    var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;    alert(res);

How to display currency in Indian numbering format

In order to generate numbers in Indian currency format, you can use toLocaleString('en-IN'). You need to change the input type of rFor1 to text.

Also, if it helps you can use keyup event instead of click.

<input  name="" type="text" placeholder="" class="form-control input-md"value="0" id="rFor1" readonly>

$("#rTpe1").keyup(function(e){
$("#rFor1").val((this.value * $("#PerHourRate1").val()).toLocaleString('en-IN'));
});

How to get numbers to Indian rupees format in web page like 10,000.00

You can use the Intl.NumberFormat method to handle it all for you pretty much, I've created the demo below.

It's similar to your existing code, except it passes the values through the number formatter first.

var formatter = new Intl.NumberFormat('en-IN', {  style: 'currency',  currency: 'INR',  minimumFractionDigits: 2,});//formatter.format(2500); /* $2,500.00 */function findTotal(){    var arr = document.getElementsByName('qty');    var tot = 0;    for(var i=0;i<arr.length;i++){        if(parseInt(arr[i].value))            tot += parseInt(arr[i].value);    }    document.getElementById('total').value = formatter.format(tot);}
<input onkeyup="findTotal()" type="text" name="qty" id="qty1"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty2"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty3"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty4"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty5"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty6"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty7"/><br><input onkeyup="findTotal()" type="text" name="qty" id="qty8"/><br>Total : <input type="text" name="total" id="total"/>

Auto format Indian currency number

You can use Intl.NumberFormat

const number = 12345678;console.log(new Intl.NumberFormat('en-IN').format(number));

I need Indian currency format like 100000 as 1,00,000 , 1234 as 1,234

The Java NumberFormat will give you what you want, but you can also write your own method:

public static String fmt(String s){

String formatted = "";
if(s.length() > 1){
formatted = s.substring(0,1);
s = s.substring(1);
}

while(s.length() > 3){
formatted += "," + s.substring(0,2);
s = s.substring(2);
}
return formatted + "," + s + ".00";
}

Test:

System.out.println(fmt("1234"));
System.out.println(fmt("100000"));
System.out.println(fmt("12345678"));
System.out.println(fmt("123456789"));
System.out.println(fmt("1234567898"));

Output:

1,234.00
1,00,000.00
1,23,45,678.00
1,23,45,67,89.00
1,23,45,67,898.00

How to format numbers as currency strings

Ok, based on what you said, I'm using this:

var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1);

var AmountWithCommas = Amount.toLocaleString();
var arParts = String(AmountWithCommas).split(DecimalSeparator);
var intPart = arParts[0];
var decPart = (arParts.length > 1 ? arParts[1] : '');
decPart = (decPart + '00').substr(0,2);

return '£ ' + intPart + DecimalSeparator + decPart;

I'm open to improvement suggestions (I'd prefer not to include YUI just to do this :-) )

I already know I should be detecting the "." instead of just using it as the decimal separator...

Number to word conversion in Indian currency format up to 16 digit number on key press

I split numbers to achieve desire result, although its cant be called as accurate one it solved my issue.

$('.allow_decimal').keyup(function (event) {
$(this).val(function (index, value) {

return value
.replace(/\D/g, '')
.replace(/(\d)(?=(\d\d)+\d$)/g, "$1,")
;
});
var money = this.value;
var length = money.length;
if (length < 15) {
$(this).closest('.form-group').find('.spnWord').text(price_in_words(this.value));
} else {
var moneys = money.replace(new RegExp(',', 'g'), '');
var lacvalue = moneys.substring(moneys.length - 7, moneys.length);
var croreValue = moneys.substr(0, moneys.length - 7);
$(this).closest('.form-group').find('.spnWord').text(price_in_words(croreValue) + ' Crore ' + price_in_words(lacvalue));
}
});

INR currency format

AngularJS

In HTML

{{ currency_expression | currency : symbol : fractionSize}}

for example

{{amount | currency:"₹":0}}

AngularJS docs Currency Pipe

Angular 2+

{{amount | currency:'INR':'symbol-narrow':'4.2-2'}}

you can also refer Currency Pipe



Related Topics



Leave a reply



Submit