"Shawn Wilson" <shawnw_nospam_ (AT) _nospam_dvigroup (DOT) net> wrote
Quote:
How do I do a simple number format in JS? Just a thousands seperator is
all I need so 1000000 becomes 1,000,000. |
Due to several recent requests on formatting a number, I wrote two
customizable functions that format decimal numbers and integer numbers.
formatFloat returns any given string or number formatted according to
settings. formatInt is merely an alias of formatFloat, that sets precision
to zero decimals. I tried to make them as robust as possible by using
try...catch and really checking the inputs... So you can give just about
anything to the function and it shouldn't trigger errors, just returns NaN.
Settings are:
var thousand_sep = ",";
var decimal_point = ".";
var default_precision = 2;
which are quite self explained, but I'll explain them anyway. thousand_sep
is the character that separates groups of three numbers. in US and England I
guess, this is ",", but from where I come from (Finland) I'd use either a
space " " or a point ".".
Next there's the decimal_point, which separates integer part of the number
from the fraction part. Again in US and English this would be "." as it is
now, but at least in Northern Europe it's ",". Enough to confuse a fellow?
Then there's the metric system versus bloody inches and gallons.
Finally the default precision, number of decimals you need in the fraction
part. Notice it can also be a negative number.
formatting 111.2222 with two decimals would return 111.22, but formatting
111.22 with -2 decimals will return 100. See how it works?
You can override the decimal setting at any time by giving the precision as
the second parameter two the formatFloat function, but it's optional.
Here are the functions. Plus there's a good set of examples below the
functions so you'll see exactly how it works. Please report any errors in
the functions to me.
<pre>
<script type="text/javascript">//<!--
var thousand_sep = ",";
var decimal_point = ".";
var default_precision = 2;
function formatFloat(aFloat, aPrecision){
try {
precision = default_precision;
if(!isNaN(aPrecision))
if(Math.abs(aPrecision)<=10)
precision = aPrecision;
} catch(e) {
precision = default_precision;
}
try {
number = parseFloat(aFloat+'');
if(isNaN(number))
return "NaN";
} catch(e) {
return "NaN";
}
number = Math.round(number * Math.pow(10, precision)) / Math.pow(10,
precision);
integerpart = '' + ((number<0) ? Math.ceil(number) :
Math.floor(number));
decimalpart = Math.abs(Math.round((number - integerpart)*(Math.pow(10,
precision))));
if(decimalpart<10)
decimalpart="0"+decimalpart;
if(decimalpart==0)
decimalpart="00";
var buff = "";
for(j=-1, i=integerpart.length; i>=0; i--, j++){
if((j%3) == 0 && j>1)
buff = thousand_sep + buff;
buff = integerpart.charAt(i) + buff;
}
if(precision>0)
return buff+decimal_point+decimalpart;
return buff;
}
function formatInt(aInt){
return formatFloat(aInt,0);
}
document.writeln(formatFloat(11.234));
document.writeln(formatFloat(11.234, 4));
document.writeln(formatInt(11.234));
document.writeln(formatFloat(65498711.234));
document.writeln(formatFloat(65498711.234, 4));
document.writeln(formatInt(65498711.234));
document.writeln(formatFloat(11));
document.writeln(formatFloat(12, 4));
document.writeln(formatInt(13.9999));
document.writeln(formatFloat('cat'));
document.writeln(formatInt('abc'));
document.writeln(formatFloat('11.23cat',-1));
document.writeln(formatInt('3456abc'));
document.writeln(formatFloat(new Date()));
document.writeln(formatFloat(120120120.21321233132 , 9));
document.writeln(formatFloat(120120120.21321233132 , 100));
//-->
</script>
</pre>
--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com