473,386 Members | 1,796 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,386 software developers and data experts.

Limit number to ####.## format

How do I ensure a number has no more than 4 digits and 2 decimal places
(adds .0 or .00 as necessary) onblur using reg expressions? Hints or
solutions appreciated.

John
Jul 23 '05 #1
8 6138
On Sun, 08 Aug 2004 23:37:39 GMT, johkar <no********@link.net> wrote:
How do I ensure a number has no more than 4 digits and 2 decimalplaces
(adds .0 or .00 as necessary) onblur using reg expressions?Hints or
solutions appreciated.


This is partially answered in the group FAQ (it's a good idea to read it
before posting). See <URL:http://jibbering.com/faq/#FAQ4_6>. The code
there add a function to Number objects, called toFixed. You call it with
the number of decimal places you want and it returns a formatted string.
For example:

var num = 5.673;
var str = num.toFixed(2); // Contains '5.67'

If the number is an integer, the returned string will have two zeros (0).

Once you've converted the number to the correct decimal format, you can
use the regular expression below to check for the correct overall format:

/^[0-9]{1,4}\.[0-9]{2}$/

For example (continued from above):

if(/[0-9]{1,4}\.[0-9]{2}/.test(str)) {
// Is of the format ####.##
} else {
// Number has five or more digits in the integer portion
}

As the decimal portion will always pass, I suppose you could use

/^[0-9]{1,4}\./

instead. The latter regular expression will probably be quicker (not by
much), though the former will be self-documenting.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #2
Thanks, I appreciate your help and the friendly reminder to check the FAQs
first.

John

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opscfuoqdbx13kvk@atlantis...
On Sun, 08 Aug 2004 23:37:39 GMT, johkar <no********@link.net> wrote:
How do I ensure a number has no more than 4 digits and 2 decimalplaces
(adds .0 or .00 as necessary) onblur using reg expressions?Hints or
solutions appreciated.


This is partially answered in the group FAQ (it's a good idea to read it
before posting). See <URL:http://jibbering.com/faq/#FAQ4_6>. The code
there add a function to Number objects, called toFixed. You call it with
the number of decimal places you want and it returns a formatted string.
For example:

var num = 5.673;
var str = num.toFixed(2); // Contains '5.67'

If the number is an integer, the returned string will have two zeros (0).

Once you've converted the number to the correct decimal format, you can
use the regular expression below to check for the correct overall format:

/^[0-9]{1,4}\.[0-9]{2}$/

For example (continued from above):

if(/[0-9]{1,4}\.[0-9]{2}/.test(str)) {
// Is of the format ####.##
} else {
// Number has five or more digits in the integer portion
}

As the decimal portion will always pass, I suppose you could use

/^[0-9]{1,4}\./

instead. The latter regular expression will probably be quicker (not by
much), though the former will be self-documenting.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail

Jul 23 '05 #3
JRS: In article <7P*****************@newsread2.news.pas.earthlink. net>,
dated Sun, 8 Aug 2004 23:37:39, seen in news:comp.lang.javascript,
johkar <no********@link.net> posted :
How do I ensure a number has no more than 4 digits and 2 decimal places
(adds .0 or .00 as necessary) onblur using reg expressions? Hints or
solutions appreciated.


In javascript, a number is stored as an IEEE Double, a floating-point
binary quantity. Your description is only meaningful when applied to a
string of characters intended to represent a number.

Decimal places are digits too; but I suppose you mean to hope for such
as '1234.56' and accept '1234.5' and '1234'. '1234.' ought not to be
allowed; it is a deprecated form. You should likewise require at least
one digit before the dot.

This, lightly tested, should check for validity :-
S = F.X2.value
OK = /^\d{1,4}(\.\d\d?)?$/.test(S)

FAQ section 4.6 (and my js-round.htm) refers to the conversion from type
Number to type String; not really applicable here. The following,
lightly tested, will sort out the decimal places :-
if (OK) S = S.replace(/(\.\d)$/, "$10").replace(/^(\d+)$/, "$1.00")

In it, $1 refers to what the parenthesised bit found. The line may not
be optimum; but it does avoid going via a Number for what is logically a
String operation.

See my js-valid.htm.
Before responding, please read section 2.3 of the newsgroup FAQ until
you understand it all. Especially paragraph 6.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
Michael Winter wrote:
How do I ensure a number has no more than 4 digits and 2
decimalplaces (adds .0 or .00 as necessary) onblur using reg
expressions?Hints or solutions appreciated.

This is partially answered in the group FAQ (it's a good idea to read
it before posting). See <URL:http://jibbering.com/faq/#FAQ4_6>. The
code there add a function to Number objects, called toFixed.


IMO, someone should take the time to write printf() and sprintf() functions
which behave in the 'standard' way. That would be really handy in js, and
instantly useable by most people with a programming background.
Any volunteers? ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #5
JRS: In article <cf********@news2.newsguy.com>, dated Mon, 9 Aug 2004
14:27:16, seen in news:comp.lang.javascript, Matt Kruse
<ne********@mattkruse.com> posted :
Michael Winter wrote:
How do I ensure a number has no more than 4 digits and 2
decimalplaces (adds .0 or .00 as necessary) onblur using reg
expressions?Hints or solutions appreciated.

This is partially answered in the group FAQ (it's a good idea to read
it before posting). See <URL:http://jibbering.com/faq/#FAQ4_6>. The
code there add a function to Number objects, called toFixed.


IMO, someone should take the time to write printf() and sprintf() functions
which behave in the 'standard' way. That would be really handy in js, and
instantly useable by most people with a programming background.
Any volunteers? ;)


I've been programming for a number of years, and AFAIR I have never used
a language with printf() or sprintf(). That casts some slight doubt on
your argument, and renders it impossible for me to code such functions
without access to a full and authoritative specification.

However, AISB, highly flexible functions need for implementation a
considerable amount of code (especially in a language which is not
ideally suited to efficient manipulation of string internals), and on a
given Web page or Web site only a small proportion of the flexibility
will usually be used.

In other words, it would be bloatware.

What can reasonably be put into a compiler's library, especially with
smart linking, is quite inappropriate in a scripting language for pages
which are likely to be distributed over slow links - into the boonies,
over the air, etc.
Via the FAQ and by other means one can find a large number of solutions
to the task of rounding a javascript Number to a string with N or 2
decimal places, some of which also format the integer part.

It might be worth examining the allied but distinct question of
"normalising" a String that represents a number, without converting
it to Number - for example converting '123.456' to '+000123.45' or to
' +123.45' or ... .

The necessary parameters seem to be places before the point, places
after, leading zeroes or spaces or not, sign or not.

For simplicity, one would either prohibit excess decimals or specify
truncation towards zero - numeric input strings should be validated
first, to deal with unreasonable input.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jul 23 '05 #6
Dr John Stockton wrote:
I've been programming for a number of years, and AFAIR I have never
used a language with printf() or sprintf().


C/C++/Perl - pretty common languages, especially among people writing
Javascript.

Delphi, Pascal, and VBScript are not languages used by most experienced
programmers ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #7
Matt Kruse wrote on 10 aug 2004 in comp.lang.javascript:
Dr John Stockton wrote:
I've been programming for a number of years, and AFAIR I have never
used a language with printf() or sprintf().
C/C++/Perl - pretty common languages, especially among people writing
Javascript.


Have you any scientific evidence [from systematic investigation] for this
claim ? I would hypothese most javascript programmers nowadays don't know
thes languages from s.., at all.
Delphi, Pascal, and VBScript are not languages used by most experienced
programmers ;)


Again I doubt your wisdom. The subjectivenesses of "experienced
programmers" and the "most" ["most programmers with experience" or
programmerds with the most experience"?] are only partly softened by the
smily.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #8
JRS: In article <cf*********@news2.newsguy.com>, dated Tue, 10 Aug 2004
15:17:25, seen in news:comp.lang.javascript, Matt Kruse
<ne********@mattkruse.com> posted :
Dr John Stockton wrote:
I've been programming for a number of years, and AFAIR I have never
used a language with printf() or sprintf().


C/C++/Perl - pretty common languages, especially among people writing
Javascript.

Delphi, Pascal, and VBScript are not languages used by most experienced
programmers ;)


Agreed, of course; it explains the poor quality of so much software.
Experience can generate wisdom, but generally does not.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Afkamm | last post by:
Hi, :) The preg_replace function... preg_replace(pattern, replacement, subject ) How on earth do you get the limit value to work with arrays? In my code both the pattern and replacement...
0
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
1
by: Erik Brown | last post by:
sql = "CALL " + myschema + ".MYSPROC(?,?,?,?,?,?,?,?,?)"; rs = con.prepareCall(sql); rs.setString(1, user); rs.setString(2, action); rs.setString(3, versions); rs.setString(4, spec);...
7
by: johnm | last post by:
We have a new CRM application that uses a DB2 7.2 database. Our users noted that the CRM application would not allow them to attach and store any documents over 2 meg in size. When asked, the...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
5
by: woof | last post by:
Hello, I have built a "contact us" form on my Web site and am using the PHP mail() function to send an e-mail to the Webmaster (that's me :-)) Everything works fine. What I would like to know...
4
by: Alec MacLean | last post by:
Is anyone aware of a size limit imposed on the subject text when using the System.Net.Mail library? I'm getting problems of message not being recieved if the subject exceeds 15 chars. Thx
1
by: dfw1417 | last post by:
I have used a query and report filter to return records related to a specific account id. I want to print a report including only the latest 6 records out of the set returned by the record filter. I...
0
by: mokoujan | last post by:
Hello all I am currently developing a WCF REST service. One of the requirements is to allow the users to search data via the rest interface. As such key field information is passed in the URL....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.