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

Help, how can you parse a decimal and ...

I am looking for the easiest way to parse a float number.

X.Y where X=>0 and X <=5 and Y = 0 or Y = 5

e.g. the number should be one of the following 1 1.0 1.5 2 2.0 2.5 etc.

I tired using regular expressions, with no luck.

Thanks,
Karim
Jul 20 '05 #1
11 16246


Karim wrote:
I am looking for the easiest way to parse a float number.

X.Y where X=>0 and X <=5 and Y = 0 or Y = 5

e.g. the number should be one of the following 1 1.0 1.5 2 2.0 2.5 etc.

I tired using regular expressions, with no luck.


I think
var numberPattern = /^[0-5]\.[05]$/;
meets your requirements

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Martin,

I tried that, but it only works for 1.0 1.5 2.0 2.5 etc but not 1 2 3 4
5. Is there another way?

Thanks,
Karim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Karim Kabbara <kk******@bu.edu> writes:
Martin,

I tried that, but it only works for 1.0 1.5 2.0 2.5 etc but not 1 2 3 4
5. Is there another way?


var numberPattern = /^[0-5](\.[05]|)$/;

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
I bow to you Lasse.Thanks!

One more addition, I am calculating three fields as-well-as validating.
I came up with the following code.

function SumTotal()
{
var raps = parseFloat(document.Form.score.value);
var lrs = parseFloat(document.Form.score1.value);
var is = parseFloat(document.Form.score2.value);

document.Form.SumTotal1.value = (raps+lrs+is);
}

function checknumeric(field)
{
pattern = /^[0-5](\.[05]|)$/;

if(pattern.test(field.value)==false)
{
alert("You can only use 1 to 5 and .5");
}
}

I would like to add the Checknumeric with each field being entered.
Would I use the function with each field or?

Thanks in advance.

Karim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Karim Kabbara <kk******@bu.edu> writes:
One more addition, I am calculating three fields as-well-as validating.
I came up with the following code.

function SumTotal()
{
I would make a variable holding the form here, to save time and
space:
var form = document.forms['Form'].elements;
var raps = parseFloat(document.Form.score.value);
and then just write
var raps = parseFloat(form.score.value);
It is not saving a lot, but I think it is easier to read :)

You want the values of these three to be between 0 and 5.5 in one-half
increments. So, you need to check each one.

I assume you want to check them before converting with parseFloat, to
rule out inputs of the form "2.5arglebargle".

For simplicity, I think I would combine the conversion and check:

function checkAndConvert(string) {
if (! (/^[0-5](\.[05])?$/).test(string)) {
alert("The input '"+string+"' is not between 0 and 5.5 in "+
"increments of one half");
return; // returns undefined
}
return +string; // converts to number faster than parseFloat.
}

Then:

function SumTotal() {

var form = document.forms['Form'].elements;

var raps = checkAndConvert(form.score.value);
var lrs = checkAndConvert(form.score1.value);
var is = checkAndConvert(form.score2.value);

if (raps === undefined ||
lrs === undefined ||
is === undefined) {
return;
}

form.SumTotal1.value = (raps+lrs+is);
}
I would like to add the Checknumeric with each field being entered.
Would I use the function with each field or?


In some way, yes.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
Lasse,

Thanks again. It works, but I am using the onChange function, so if I
change each field it calculates all the fields, I need to check the
validation on !this field then add to the total everytime I change to
the next field.

The algorithim would be validate num with rule 0 to 5 with .5
increments. Then add and total the num.

Do you think I need an array to do this?

Regards,
Karim
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7
Oh yes, I forgot to add, I should use the focus function if the num is
not valid. So when they change focus and it does not satisfy the
condition, it place's them back on the field to change.

Karim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #8
Karim Kabbara <kk******@bu.edu> writes:
Oh yes, I forgot to add, I should use the focus function if the num is
not valid. So when they change focus and it does not satisfy the
condition, it place's them back on the field to change.


This is a dangerous thing to do. It traps people in the field. Good
thing it isn't the onblur handler, since this way, they can get away
from the input field by trying to leave it twice (the second time,
there is have been no change, so the onchange handler doesn't
trigger). Don't change it to onblur!

You can collect all the values each time you want to update the result,
or you can cache them. Here I cache them in an object instead of in an
array, together with their sum.

---
var values = {raps:0,lrs:0,is:0,total:0};

function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {
alert("Input illegal: "+elem.value);
setTimeout(function(){elem.focus();},20); // [1]
return;
}
var value = +elem.value;
values.total += value - values[elem.name];
values[elem.name] = value;
elem.form.elements['SumTotal1'].value = values.total;
}
---
Then you can call this function from each element:
---
<input type="text" name="raps" value="0" onchange="checkAndAdd(this)">
<input type="text" name="lrs" value="0" onchange="checkAndAdd(this)">
<input type="text" name="is" value="0" onchange="checkAndAdd(this)">
---
[1] The delay is for browsers which change the focus to the next
element *after* executing the onchange event handler, overriding
the focus set in the handler.

Hope this works :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9
JRS: In article <3f******@olaf.komtel.net>, seen in
news:comp.lang.javascript, Martin Honnen <Ma***********@t-online.de>
posted at Sat, 27 Sep 2003 16:11:40 :-
Karim wrote:
I am looking for the easiest way to parse a float number.

X.Y where X=>0 and X <=5 and Y = 0 or Y = 5

e.g. the number should be one of the following 1 1.0 1.5 2 2.0 2.5 etc.

I tired using regular expressions, with no luck.


I think
var numberPattern = /^[0-5]\.[05]$/;
meets your requirements


That seems to fit the description.
var numberPattern = /^[1-5](\.[05])?$/; // fits the example

I wonder whether he actually wants 5.5 ?

--
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 20 '05 #10
Yes, I am not trying to get 5.5 my max is 5. Also, I tried the code and
I am getting Object not found. The sample is below.Thanks

<script language="Javascript">
var values = {raps:0,lrs:0,isc:0,total:0};

function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {
alert("Input illegal: "+elem.value);
setTimeout(function(){elem.focus();},20); // [1]
return;
}
var value = +elem.value;
values.total += value - values[elem.name];
values[elem.name] = value;
elem.form.elements['SumTotal1'].value = values.total;
}
</script>
<table>
<tr>
<td>RAPS</td>
<td><input maxlength="10" size="10" value="" name="raps"
onchange="checkandadd(this)"></td>
</tr>

<tr>
<td >LRS</td>
<td ><input maxlength="10" size="10" value="" name="lrs"
onchange="checkandadd(this)"></td>
</tr>

<tr>
<td >IS</td>
<td ><input maxlength="10" size="10" value="" name="isc"
onchange="checkandadd(this)"></td>
</tr>

<tr>
<td align="right"><span style="FONT-SIZE: 12pt">Overall
Score (0-15):
<o:p></o:p>
</span> </td>
<td ><strong><input id="SumTotal1" readonly name="SumTotal1"
style="WIDTH: 79px; HEIGHT: 22px"

size="10"></strong></td>
</tr>

</table>

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #11
Karim Kabbara <kk******@bu.edu> writes:
Yes, I am not trying to get 5.5 my max is 5. Also, I tried the code and
I am getting Object not found. The sample is below.Thanks <script language="Javascript">
Use
<script type="text/javascript">
instead. The type attribute is required in HTML 4.
var values = {raps:0,lrs:0,isc:0,total:0};

function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {
If you didn't want 5.5, the regular expression would have to change, e.g.,
if (!/^(([0-4](\.[05])?)|(5(\.0)?))$/.test(elem.value)) {

or perhaps using a simple comparison with the regular expression:

if (!/^[0-5](\.[05])?$/.test(elem.value) || elem.value > 5) { elem.form.elements['SumTotal1'].value = values.total;
There is no form around your input elements (which I assumed there
would be), so this fails. Have to change it to:

document.getElementById("SumTotal1").value = values.total;
onchange="checkandadd(this)"></td>
Javascript is case sensitive. This should be

onchange="checkAndAdd(this)"></td>

with capital A's.
This goes for all three inputs.
<td ><strong><input id="SumTotal1" readonly name="SumTotal1"
style="WIDTH: 79px; HEIGHT: 22px"


You don't need the name attribute on this input. Also, the strong tag
isn't affecting the contents of the input in my browser. You would
probably get a better result by adding "font-weight:bold;" to the style
attribute.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12

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

Similar topics

8
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
3
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be...
2
by: probashi | last post by:
Hi, I am trying to the following: String s = "( 54.05)"; decimal d = decimal.Parse(s); when s = "( 54.05)" I what the value -54.05 and s = " 54.05" I what the value 54.05
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
1
by: Mythran | last post by:
Dim value As Decimal = 1234.56 Dim format As String = _ "Positive $#,##0.00;Negative $#,##0.00;" Dim s As String = value.ToString(format) Dim d As Decimal = Decimal.Parse(s) The last line...
13
by: Jason | last post by:
Could someone here show me how I would write a vb program to convert decimal ip address to binary? For example a small form with a convert button and a label for the result and a textbox for the...
2
by: PJHORNSA | last post by:
Hi all, I got a problem.. I want to format the time into a decimal value.. The code that I used worked fine for some time but now it does not want to work.... If any one can help me or give me...
5
by: Navid Azimi | last post by:
What's the best way to parse a currency string to a decimal given the possibility of multiple currencies? That is, my numeric string can be ($12.00) or -12.00; in either case I want -12.00 to be...
3
by: vanidosa27 | last post by:
I need to write a code that calculates the change due after the user has input the amount owed and amount paid. This is easy enough! :rolleyes: My problem comes in when the program wants you to...
3
by: Andrew Wrigley | last post by:
Hi, I have a string: "100.00" ....that I want to parse into a decimal. So: decimal d = decimal.Parse("100.00", System.Globalization.NumberStyles.Any) On my dev machine (United Kingdom),...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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:
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...

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.