473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regular Expression Help in formatting number

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
2 1592
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris Lasher | last post by:
Hello, I would like to create a set of very similar regular expression. In my initial thought, I'd hoped to create a regular expression with a variable inside of it that I could simply pass a...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
1
by: AndiSmith | last post by:
Hi, I'm adding some old database fields to view in our new C# .NET system with cascading stylesheets, and unfortunately the old data has HTML formatting in there which I would like to be removed...
6
by: rorymo | last post by:
I have a regular expression that allows only certain characters to be valid in an xml doc as follows: <xs:pattern value="^*" /> What I want to do is also allow any unicode character that is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.