HighDots Forums  

Javascript number format...

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Javascript number format... in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Shawn Wilson
 
Posts: n/a

Default Javascript number format... - 08-31-2005 , 03:13 PM






How do I do a simple number format in JS? Just a thousands seperator is all
I need so 1000000 becomes 1,000,000.

Thanks!

---------
Shawn Wilson



Reply With Quote
  #2  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: Javascript number format... - 09-01-2005 , 01:31 AM






"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




Reply With Quote
  #3  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: Javascript number format... - 09-01-2005 , 01:35 AM



"Kimmo Laine" <eternal.erectionN05P (AT) Mgmail (DOT) com> wrote

Quote:
"Shawn Wilson" <shawnw_nospam_ (AT) _nospam_dvigroup (DOT) net> wrote in message
news:vboRe.37656$kM5.27423 (AT) fe01 (DOT) news.easynews.com...
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 ...
whoops! In the actual function: look out for the long lines that are broken
into two lines. There were a few due to length of the statements and
indenting... Might result errors if not fixed.

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com




Reply With Quote
  #4  
Old   
Jasen Betts
 
Posts: n/a

Default Re: Javascript number format... - 10-15-2005 , 07:32 PM



On 2005-08-31, 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.
function commafy(val){
var pos;
val=val.split(",").join(""); // remove existing commas if present.
var dot=val.indexOf("."); // locate decmal
if(dot<0)dot=val.length; // use end if no decimal
var r="";
for(pos=dot-3;pos>=1;pos-=3) // put commas in
r=","+val.substr(pos,3)+r;
r=val.substring(0,pos+3)+r; // put start of string on
dot=val.indexOf("."); // check for decimal
if(dot>0)r+=val.substring(dot);// put fraction part on
return r;
}

dunno how popular that will be with the europeans who use . in place of ,
and , in place of .


Bye.
Jasen


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.