This function will round numbers to two decimal places, and ensure that the returned value has two decimal places. For example 12.006 will return 12.01, .3 will return 0.30, and 5 will return 5.00
function CurrencyFormatted(amount) {
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}
If you want a smaller function:
If you want a smaller, more inline alternative, you can do this:
-Chase
Hey, here’s myself from the future. I re-gazed upon this, and further improved things:
function currency(n){n=parseFloat(n);return isNaN(n)?false:n.toFixed(2);}
Is it a css trick?
Hi all!!
Is there any way to add comma while typing number? and we can edit all of number.
Ex: 123,456,555, we can edit number 1 –> 6
Thanks
On keyup check the length of the field. If it is greater than 4 and contains no period, add a comma after every 3rd number from the right. When a period is encountered, set a flag to stop the formatting.
// with comma for thousands
function format_currency(amount){
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = ”;
if(i < 0) { minus = ‘-‘; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = Number(i).toLocaleString(‘en’);
if(s.indexOf(‘.’) < 0) { s += ‘.00’; }
if(s.indexOf(‘.’) == (s.length – 2)) { s += ‘0’; }
s = minus + s;
return s
}
The website’s name is css-tricks but you give a Java solution?
http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java
He didn’t give a java solution, he gave a javascript solution. ;)
thanks
i want to put auto price changer to my website
please review my website: http://www.phonegallery.in
Personally, I prefer to use currencyFormatter.js (https://osrec.github.io/currencyFormatter.js/). Cross browser and light. Can format numbers according to currency and/or locales:
function formateIndianCurrency(rupees) {
}
as of 2018, this functionality is built in to
Number.toLocaleString
There is an additional option
currencyDisplay
which defaults tosymbol
and shows the appropriate symbol for thecurrency
.I have multiple Javascript variables, inputs and outputs. I am thinking it would be easier to format the outputs in case I later want to use the variable again for another calculation. (Or can you confirm that it wouldn’t in anyway confuse the calculation trying to multiply ‘12345’ with ‘12,345.00’ due to the comma and period symbol? 12345 * 12,345.00 = ?)
I am not sure how to implement this currency format. How can I pass my output, for example: through the currency format? The calculations for MyOutputAmount has already been done and I am using ‘spans’ for the outputs.
A comma in the value means that it is a string. You want to use these functions only for displaying the value, not when there are further calculations to make.
Ahh yes of course. That makes perfect sense to be honest.
However how would I apply this function to my span outputs?