# Thread: Numbers in a form

1. ## Numbers in a form

Another quick question. I have the script to add each field up I need, but if the ending 0 after a decimal is present it doesnt show. How do I keep the leading zero and how do I keep just 2 digits after the decimal?

For example:

1 + 1.50 = 2.50

But javascript displays it as 2.5

Now sometimes when I do math I get:

2.3454545

In this case I just want 2.34. So if there is a ending zero I want to keep it, and if there is more than 2 numbers after the decimal I want to cut the rest off and leave the 2 there. Any suggestions?

Thanks
Silly

2. I would like to know the answer to this one aswell? Where is beetle when you need him? We need some sort of a signal like batman had!

3. This may be more than you asked for; but what the hay, I can't spend that much time recoding things I already wrote:

Code:
```<script type="text/javascript">
<!--
function num2dollar(fldObj)
{
var tmp1 = cents = dollars = "";
var dec = -1;
var num = i = 0;
num = fldObj.value;
num = "" + (Math.round(num * 100)/100);
dec = num.indexOf(".");
cents = ((dec > 0) ? num.substring(dec,num.length) : ".00");
if (cents.length == 2) cents += "0";
dollars = "" + parseInt(num);
tmp1 = insComma(dollars);
num = "\$";
for (i = tmp1.length-1; i >= 0; i--)
num += tmp1.charAt(i);;
num +=  cents;
fldObj.value =  num ;

return(true);
}
function insComma(data)
{
var count = i = 0;
var tmpStr = "";
var comma = ",";
for (i = data.length-1; i >= 0; i--)
{
if (count == 3)
{
tmpStr += comma;
count = 1;
}
else  count ++;
tmpStr += data.charAt(i);
}
return(tmpStr);
}
//-->
</script>
<body>
<form name="a">
<input type="text" onblur="num2dollar(this)">
</form>```
Vinny

4. I have done some research into this subject and found a few interesting details! There are three Number object methods that lets a script control the formatting of numbers for displaying as text strings! Each method has a unique purpose, and all return a string formmat. Only after all math calculations have been done on the values should you use these methods! The three methods are as follows:

number.toExponential(fractionDigits);

The toExponential() method forces a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation. The parameter is an integer specifiying how many digits to the right of the decimal are returned, even if they are zeros!

number.toFixed(fractionDigits);

Use the toFixed() method when you want to format a number with a specific number of digits displayed to the right of the decimal! This is the method you use to display the results of some sort if calculations using hundredths of units(IE.. dolors and cents). The parameter to the method is an integer indicating the number of digits to be formatted to the right of the decimal. If the number being formatted has more numbers to the right of the decimal than the number of digits specified in the parameter, the method rounds the number to the rightmost visible digit. But only with respect to the unrounded value of the next digit.

number.toPrecision(precisionDigits);

The third method is toPercision(), which enables you to define how many total digits(including digits on both the left and right of the decimal) to display of a number. In other words, you define the percision of a number.

Jon

5. Hi Jon,
I have done some research
Where?
Have you tried any of those 'methods'? When I tried 'toPercision' all I got was an 'object does not support this method' error message.

Vinny

6. oh Sorry I just did some reading in a book of mine! I have not done any testing! I read that it was both IE5.5+ and NS6+ compatible! Sorry I will do some testing with it after I get off my stupid job!

Jon

7. This is odd sometimes they seem to work and other times they do not! I am confused, also when they do work they return odd values like theses?

1E+2

8. AHH H AHAHHAHA

This is driving me batty WTF! Where is the human javascript archive when you need them? Help?

Code:
```<script>
var d=document.theFrm;
var x=d.oper.options[d.oper.selectedIndex].value;
var val1=parseFloat(val_1);
var val2=parseFloat(val_2);
var aws=eval(val1+x+val2);
var txt=parseFloat(aws);
var txt1=txt.toExponential(7);
d.ans1.value=txt;

}
</script>

<form name="theFrm">
<input type="text" name="val1">
<select name="oper">
<option value="*">*
<option value="-">-
<option value="+">+
<option value="%">%
<option value="/">/
</select>

</form>```

9. Ok an answer as been found! I had put toPercision() it should be toPrecision()! My mistake!

Hey Vinnie, This should work fine now!
Code:
```<html>

<title>Numbers</title>

<script type="text/javascript"><!--

-->
</script>

<body>

<form>

<input type="text" id="num" value="145.4540"><br><br><hr><br>

<input type="button" onclick="tf();" value="toFixed"><br><br>

<input type="button" onclick="pf();" value="parseFloat"><br><br>

<input type="button" onclick="tp();" value="toPrecision"><br>

</form>

</body>

</html>```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•