4 Replies - 774 Views - Last Post: 25 September 2012 - 12:50 PM

#1 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

round up numbers

Posted 25 September 2012 - 10:55 AM

I am trying to round up the results for this block of JS code. I tried Math.round, and it did not work, or may be I don't know where and how to use this. Can you please advise me on how to round up the result for "hoursOfVacationPerMinute".
Currently if I run the code, I get 0.0009132420091324201 for hoursOfVacationPerMinute
Thanks in advance.


hoursOfVacationPerMinute = totalVacationHours / dividend;
document.write("Hours of vacation per Minute:     " +hoursOfVacationPerMinute+ "</br>");





Is This A Good Question/Topic? 0
  • +

Replies To: round up numbers

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: round up numbers

Posted 25 September 2012 - 11:15 AM

Math.round() works to round up/down to nearest integer (aka whole number). You can use the toFixed() method to specify rounding to a decimal place...

(0.0009132420091324201).toFixed(3)



This would give you .001 or your number rounded to the thousandths place. Now keep in mind that Javascript stores numbers as floating point so some numbers can't be represented EXACTLY. If you are using this for extremely precise number calculations, you will need something else. However, this method can serve you in getting some approximations.

:)

This post has been edited by Martyr2: 25 September 2012 - 11:22 AM

Was This Post Helpful? 1
  • +
  • -

#3 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

Re: round up numbers

Posted 25 September 2012 - 11:45 AM

Thanks for the reply Martyr2.

Is this the right way to assign it. if it is, the output on the screen is blank this time.

document.write("Hours of vacation per Minute:     "(hoursOfVacationPerMinute5).toFixed(2));


Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: round up numbers

Posted 25 September 2012 - 12:20 PM

there is the concatenation missing.
Was This Post Helpful? 1
  • +
  • -

#5 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

Re: round up numbers

Posted 25 September 2012 - 12:50 PM

Thanks again. It worked!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1