Click to See Complete Forum and Search --> : Number formats
code_monkey
02-15-2003, 09:45 PM
How do I specify retaining decimal placement in a number? For example, if I multiply 24.6 * 10, I want to end up with 246.00, not 246. I never know if the resulting answer will be a whole number as above, or will contain decimal places (24.6 x 11.5 = 282.90, NOT 282.9).
I always need two decimal places in the answer, even if they're zeros, because I have to convert to answer to a string and then substring the separate integers into different places on a form.
code_monkey
02-15-2003, 10:06 PM
Oh yeah, right.....
I'll give it all a try. After all, three days ago I had never ever written JavaScript... ;-)
Thanks.
code_monkey
02-15-2003, 10:53 PM
parseInt, parseFloat, string, substring, charAt, length... the normal stuff for going from strings to numbers and back again...
I think I can see a way to use indexOf and split, but it'd be too long to show you here. I'll give it a try and let you know. (My expertise is in OOP with dBase, so at least I'm familiar with methods, properties, etc.)
Charles
02-16-2003, 12:22 AM
<script type="text/javascript">
<!--
Number.prototype.toFixed = function (places) {
if (places > 20 || places < 0) return;
places = Math.round (places);
var n = (Math.round(Math.abs(this) * Math.pow(10, places))).toString();
for (var i=1; i<=places; i++) {if (n < Math.pow(10,i)) n = '0' + n};
if (this < 0) n = '-' + n;
return n.slice(0, -(places)) + '.' + n.slice(-(places));
}
alert (Math.PI.toFixed(4));
// -->
</script>
khalidali63
02-16-2003, 10:04 AM
Just a bit of addition.
Number.toFixed(decimalLimit)
is not only IE specific.It was implemented in JavaScript 1.5,hence it can be used any browser that implements JavaScript version 1.5(NS6+ browsers).
This is how it may be used
Number.toFixed(numberOfDecimals)
e.g
var x = 3.34;
var y = 9.45
document.write("x * y = "+(x*y).toFixed(2));
will display
x * y = 31.56
cheers
Khalid
code_monkey
02-16-2003, 10:10 AM
Hey guys, I figured it out using indexOf and Split, and it worked quite well. Thanks for all the help. And Dave, it wasn't a class assignment. It was for real. I'm rewriting an old dBase DOS database systems for Windows, and one of the problems was converting some forms to PDF and making them fillable on the screen. Making PDFs is a piece of cake, but some of the forms have separate boxes for entering a number (like you see on some machine-reabable forms). The forms couldn't be changed -- they're approved by a state department of insurance. I've written code for many years, just not JavaScript. Logic is still the same -- it was the specific command language and syntax I didn't know. Anyway, thanks again.
Charles
02-16-2003, 12:36 PM
Dave,
It didn't seem to me that code_monkey was trying to cheat on his or her homework. But you are right; I should have taken into account that some browsers suport Number.toFixed():
<script type="text/javascript">
<!--
if (!Number.prototype.toFixed()) Number.prototype.toFixed = function (places) {
if (places > 20 || places < 0) return;
places = Math.round (places);
var n = (Math.round(Math.abs(this) * Math.pow(10, places))).toString();
for (var i=1; i<=places; i++) {if (n < Math.pow(10,i)) n = '0' + n};
if (this < 0) n = '-' + n;
return n.slice(0, -(places)) + '.' + n.slice(-(places));
}
alert (Math.PI.toFixed(4));
// -->
</script>