473,804 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(nu m);
while(fmt.test( myNum))
{
myNum = myNum.replace(f mt, "$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 1623
JRS: In article <b1************ *****@newsread1 .news.pas.earth link.net>,
seen in news:comp.lang. javascript, johkar <no********@lin k.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.demo n.co.uk/js-maths.htm#OutCo mma>
<URL:http://www.merlyn.demo n.co.uk/js-maths.htm#RComm a>

--
© 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.demo n.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #2
Thanks for the help.

John

"Dr John Stockton" <sp**@merlyn.de mon.co.uk> wrote in message
news:4y******** ******@merlyn.d emon.co.uk...
JRS: In article <b1************ *****@newsread1 .news.pas.earth link.net>,
seen in news:comp.lang. javascript, johkar <no********@lin k.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.demo n.co.uk/js-maths.htm#OutCo mma>
<URL:http://www.merlyn.demo n.co.uk/js-maths.htm#RComm a>

--
© 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.demo n.co.uk/js-index.htm> Jsc maths, dates, sources. <URL:http://www.merlyn.demo n.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
8297
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 string into by defining this variable elsewhere in my module/function/class where I compile the regular expression, thus making for an easy substitution. However, still after JFGI'ing, I still cannot find the syntax to place a variable inside a...
1
4187
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
5
19234
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 pretty limited. I wrote the following expression to take (hopefully) any _reasonable_ phone number input, and format it as (999) 999-9999 x 9999.
3
3223
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 that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
2
3020
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 index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and otherwise ignored. I decided to use the following regular expression to remove all punctuation (actually...
7
3833
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5178
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
11
3116
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 a hand? import regsub
1
2578
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 for these pages as this will be mainly driven by the CSS. I know the Regular Expression to remove all HTML tags, but would anybody happen to know a regular expression that could replace all HTML tags except bold, italic, underline and breaking...
6
2866
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 enclosed in single quotes to also be valid, no matter where they appear. I tried the following: <xs:pattern value="^*('*)*" />
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.