Anyone know a bit of Javascript?

Budo

Raging Clue
Sep 19, 2007
726
9
Canadia
I have a form where the user inputs a number, selects a size, and then the total is displayed in a third box. The problem is, I want the total to be formatted to two decimal places. The answer has to be simple, but I can't find anything:

<script language = JavaScript>

function calculate()

{

A = document.form1.txtFirstNumber.value
B = document.form1.txtSecondNumber.value

A = Number(A)
B = Number(B)
C = (A * B )
document.form1.txtThirdNumber.value = C

}

</script>
 
Is the Javascript parser later than 1.5? Then you can use a nice built-in function: toFixed()

Like so:

var v = 25.567;
return v.toFixed(2);

(which will return 25.56)

Again, this is specifically for later JS implementations.

Oh BTW, if you need a function for earlier JS, I'm sure I have something - I used to use the Math.Round() function (which rounded up to the nearest int) with a little creative math to pad the decimal places (simple * and / by a factor of 10).

Also you might want to check for a numeric value (isNan(), or parse out the numeric portion parseInt(), or parseFloat()).
 
Last edited:
Budo said:
It's after 1.5.

Thanks, I'll give that a try


No problem.

You might also want to use the later DOM for finding elements vs. having to traverse a tree:

document.getElementById()

Then you can short form the element/object like so:

var f_name = document.getElementById('TextBoxFirstName');

var f_val = f_name.value;

f_val.style.display = 'block';

Handy if you need to get/set properties for the element in a few places.
 
Back
Top