Closed Thread Icon

Preserved Topic: rounding numbers in JavaScript calculations (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17831" title="Pages that link to Preserved Topic: rounding numbers in JavaScript calculations (Page 1 of 1)" rel="nofollow" >Preserved Topic: rounding numbers in JavaScript calculations <span class="small">(Page 1 of 1)</span>\

 
Babamba
Paranoid (IV) Inmate

From: my mother
Insane since: Aug 2000

posted posted 08-17-2000 05:03

I am learning JavaScript and for practice, I wrote my own little script for my website when it is done. There is a problem though. Go to http://members.tripod.com/~Babamba/javascript.htm and type in these numbers: 48, 12, 22 . Is there any way that I can make the huge number round to the nearest hundredth?(spelling?) thanks a lot for your time.


~Aaron

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 08-17-2000 08:49

rounded_number = math.Round(num*100)/100;

That should do it, I'd think. Slime, comments?

Your pal, -doc-

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 08-17-2000 09:00

OK, I got my CAPitalIZAtion off, it's actually Math.round(...). This is one of those picky tags, that needs to be capitalized just this way. I also had some problems with netscape, turns out "long" is a reserved word, heh. I ended up with this code...

<Script Language="JavaScript">
<!--

var length = prompt("How long is your tank?" , "Type the length of your tank in inches");
var width = prompt("How wide is your tank?" , "Type the width of your tank in inches");
var height = prompt("How tall is your tank?" , "Type the hight of your tank in inches");
capacity = Math.round(((length * width * height) / 231)*100)/100;
var capacity = "Your aquarium holds " + capacity + " gallons";
alert(capacity);

//-->
</Script>


This worked for me, on both Netscape and IE.

Your pal, -doc-

Babamba
Paranoid (IV) Inmate

From: my mother
Insane since: Aug 2000

posted posted 08-17-2000 16:25

Thank you for the help. It works great.


~Aaron


ocb
Bipolar (III) Inmate

From: Dublin, Ireland
Insane since: Jun 2000

posted posted 08-17-2000 16:26

Also, Math.round rounds the number up. Math.floor rounds the number down.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-17-2000 18:26

No, Math.round correctly rounds the numbers, I believe. If you want to round up, use Math.ceil() (short for ceiling, i believe). And Math.floor() does, as you said, round down.

The above script that doc provided should work. Are there 231 cubic inches per gallon? Or is that number for something else?

<anguish volume="3">
Augh! This is off the topic, but I just realized that there is a Math.atan2() function, and all this time i've been writing my own!!! What a waste of time! </anquish>

Babamba
Paranoid (IV) Inmate

From: my mother
Insane since: Aug 2000

posted posted 08-17-2000 19:22

Correct. 231 cubic inches = 1 gallon. thanks for the help. I've also posted a brief bio here http://www.ozones.com/forum/Forum1/HTML/000258.html

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 08-17-2000 23:44

Yah, I use the Math.floor() function when I'm dealing with random numbers, it helps me be sure not to round the wrong way and get a number one too big. I could get the same effect with the "round up" function, but I always have a hard time spelling that word for the underside of the roof, heh.

"i before e, except after c..." <img border=0 align=absmiddle src="http://www.ozones.com/forum/smile.gif">

Your pal, -doc-

« BackwardsOnwards »

Show Forum Drop Down Menu