By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Regular Expression Help in formatting number

P: n/a
I picked up the following script from an old post on this newsgroup. It
works fine for numbers to two decimal places, but I have a need to format
numbers to three decimal spaces from this: 1234.394 to this: 1,234.394 (no
rounding). I don't quite follow the reg exp in the variable fmt or what the
$1 and $2 do in the myNum replace method. Any enlightenment/help would be
appreciated.

Thanks, John

//FORMATS NUMBERS WITH COMMAS.
function formatNum(num){
var fmt=/(-?\d+)(\d{3})/;
var myNum=String(num);
while(fmt.test(myNum))
{
myNum = myNum.replace(fmt, "$1,$2");
}
return myNum;
}

Also I need to limit the user to no more than 3 decimal places as they type
or onchange. Opinions on the best way to accomplish this. IE only ok, but
would like it to work for Mozilla/NS 7 too.

Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
JRS: In article <b1*****************@newsread1.news.pas.earthlink. net>,
seen in news:comp.lang.javascript, johkar <no********@link.net> posted
at Thu, 18 Dec 2003 01:03:03 :-
I picked up the following script from an old post on this newsgroup. It
works fine for numbers to two decimal places, but I have a need to format
numbers to three decimal spaces from this: 1234.394 to this: 1,234.394 (no
rounding). I don't quite follow the reg exp in the variable fmt or what the
$1 and $2 do in the myNum replace method. Any enlightenment/help would be
appreciated.


<URL:http://www.merlyn.demon.co.uk/js-maths.htm#OutComma>
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#RComma>

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #2

P: n/a
Thanks for the help.

John

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:4y**************@merlyn.demon.co.uk...
JRS: In article <b1*****************@newsread1.news.pas.earthlink. net>,
seen in news:comp.lang.javascript, johkar <no********@link.net> posted
at Thu, 18 Dec 2003 01:03:03 :-
I picked up the following script from an old post on this newsgroup. It
works fine for numbers to two decimal places, but I have a need to format
numbers to three decimal spaces from this: 1234.394 to this: 1,234.394 (norounding). I don't quite follow the reg exp in the variable fmt or what the$1 and $2 do in the myNum replace method. Any enlightenment/help would beappreciated.
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#OutComma>
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#RComma>

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics,

links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.