Click to See Complete Forum and Search --> : Converter to return correct # of decimals.
big_unit
02-05-2003, 08:45 AM
So, I have this distance calculator in a site we've built. You can view what I mean at:
http://touryukon.com/english/RoadTravel/roadtrips_rt_conv.asp?Nav=rtnav_conv&SectionContent=rt_conv
I'm sorry, but I cannot remember how to teach that little script how to stick to two (2) decimal places.
Can somebody help me?
AdamGundry
02-05-2003, 09:04 AM
I believe you can use Math.round(), which rounds to the nearest integer, like this:
var i = 25.674;
i = Math.round(i*100)/100;
// i should now be 25.67
Adam
Dan Drillich
02-05-2003, 09:11 AM
From "JavaScript - The Definitive Guide"
A shortcoming of JavaScript prior to 1.5 is that there is no built-in way to convert a number to a string and specify the number of decimal places to be included....
ECMAScript v3 and JavaScript 1.5 solve this problem by adding three new number-to-string methods to the Number class. toFixed() converts a number to a string and displays a specified number of digits after the decimal point....
i.e. n.toFixed(2);
Cheers,
Dan
big_unit
02-05-2003, 09:12 AM
Thanks Adam, but how can I work your solution into this snippet:
<input type="text" name="Miles" size="10" value="1"
onChange="Kilometers.value = 1.61 * this.value"> </TD>
<TD bgcolor=#003366 align=center> <input type="text" name="Kilometers" size="10" value="1.61"
onChange="Miles.value = 0.621 * this.value"> </TD>:confused:
Charles
02-05-2003, 10:29 AM
<script type="text/javascript">
<!--
Math.centiRound = function (num) {return Math.round(num*100) / 100}
// -->
</script>
<input type="text" name="Kilometers" size="10" value="1.61" onChange="Miles.value = Math.centiRound(0.621 * this.value)">
or more simply
<input type="text" name="Kilometers" size="10" value="1.61" onChange="Miles.value = Math.round(0.621 * this.value * 100) / 100 )">
big_unit
02-21-2003, 08:22 AM
Nope, none of the suggestions worked. Again, if anyone can help me, that'd be great!
The URL to try out the simplest of converters is at
http://www.touryukon.com/english/RoadTravel/roadtrips_rt_conv.asp?Nav=rtnav_conv&SectionContent=rt_conv
...for those of you who could maybe help me!
Charles
02-21-2003, 08:53 AM
It works just fine for me; you must be doing something wrong.
By the way, you have made a form that relies upon JavaScript and JavaScript will fail one or more times in ten. And you may, thereby, have violated accessibility laws. See http://www.evolt.org/article/Accessibility_Laws_In_Canada/4090/28074/.
big_unit
02-21-2003, 08:58 AM
Hey, that was pretty funny, that article.
Anyway....because I'm probably more of a newbie than not, is there any chance I could email you the code? It's not even 50 lines in total, so it shouldn't take you long.
I don't believe I've violated anything, actually...but could be wrong.....thanks!
BTW, did you get pretty snowed-out last week?
khalidali63
02-21-2003, 09:04 AM
Is browser version a problem for you?
if YOu have standard for IE5+ and NS6+ then the solution is as simple as it can get.
say you have a number = 12.3569;
you can use JavaScript Number.fixed() or Number.precision() methods.
lets use fixed().
alert(number.fixed(2))
will result
12.35
Cheers
Khalid
big_unit
02-21-2003, 09:34 AM
Hey, Khalid....cold enough for ya out there!?
How can I put your solution into my script?
khalidali63
02-21-2003, 09:52 AM
:-)
How does -25 F sounds...
put this function anywhere in the script tags.
function formatNum(num,decimals){
return num.fixed(decimals);
}
whereever you call this method with the 2 parameters
1. the number you want to format and
2.decimals will be the decimal ponts you want to allow.
e.g
if you have a variable
var total = 12.3456;
and you wanted to get 2 decimal points precision
then the pass these values to the function above.
alert( formatNum(total,2));
Hope this helps
Cheers
Khalid