Click to See Complete Forum and Search --> : Working with numbers and decimal points


nuthead
05-28-2003, 07:29 AM
I have a script that will dynamically work out the cost of an item depending on the quantity (1 = £1, 2-4 = £0.75, 5+ = £0.60p) this all works perfectly fine except for certain numbers but say I wanted 2@75p it would give me a value of 1.5 rather than 1.50. What I need is it to make all numbers show to two decimal places, is this possible?

Thanks in advance

pyro
05-28-2003, 07:34 AM
You will want to use toFixed(). Try this:

<script language="javascript" type="text/javascript">
num = 1.5;
num = num.toFixed(2); // this is the line you will want
alert (num);
</script>

nuthead
05-28-2003, 07:38 AM
Thanks pyro for your lightning fast response there, it worked :D

pyro
05-28-2003, 07:39 AM
You bet... ;)