3

Possible Duplicate:
javascript - ceiling of a dollar amount

I have a number like this: 360.654444444447

I want this to round up to 360.66

How do i do it? The amount of 4's between the 5 and 7 is unknown.

EDIT: The key issue here is that when the decimals after the 2nd would round to 5, it should round up. (ie: 360.654447 can be rounded to 360.655 - and that should round to 360.66

It's similar to the PHP_HALF_ROUND_MODE thing.

Community
  • 1
  • 1
ragulka
  • 4,312
  • 7
  • 48
  • 73
  • 1
    There is a very similar question here - http://stackoverflow.com/questions/5191088/how-to-javascript-round-up-number – Vishnu Haridas Jan 11 '12 at 17:22
  • Possibly even more similar: http://stackoverflow.com/questions/4817296/javascript-ceiling-of-a-dollar-amount – Tyler Crompton Jan 11 '12 at 17:26
  • So you want 3.5446 to "round" to 3.55 even though 3.54 is closer to the original value than 3.55 (.0046 less as opposed to .0054 more)? Why would you want to do that? – Trott Apr 12 '13 at 23:11

1 Answers1

1

Use the ceil method, not the round method as others have suggested.

var number = 360.654444444447;
var result = Math.ceil(number * 100) / 100;
Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94
  • That comes with a problem. I don't want numbers like 360.2234002 to round up to 360.23... The key issue here is that when the decimals after the 2nd would round to 5, it should round up. (ie: 360.654447 can be rounded to 360.655 - and that should round to 360.66 – ragulka Jan 11 '12 at 18:37
  • But the thousandths place wouldn't round to 5 in that 360.654447... Unless you are talking about rounding multiple times... May I ask why you are trying to do this weird rounding? – Tyler Crompton Jan 12 '12 at 03:44
  • Are you talking about rounding to the nearest half value (i.e. 4.1=>4.0, 4.3=>4.5, 4.6=>4.6, 4.8=>5.0)? – Tyler Crompton Jan 12 '12 at 03:50
  • Nope, I'm talking about rounding half up. – ragulka Jan 12 '12 at 08:00
  • Please provide more example numbers so that I can get an idea as to what you are talking about. – Tyler Crompton Jan 12 '12 at 17:55