3

Lets say I have an number which is equal to 288.65 and I want to multiply out the decimal point to achieve the desired result of 28865

If I simply just try say console.log(288.65 * 100) it will return 28864.999999999996 I am not sure why it is doing this, any help will be much appreciated.

Vassilis
  • 818
  • 3
  • 12

1 Answers1

2

You can use Math.ceil and with Math.round

The Math.ceil() function always rounds a number up to the next largest integer.

The Math.round() function returns the value of a number rounded to the nearest integer.

const num = 288.65;

const result1 = Math.round(num * 100);
const result2 = Math.ceil(num * 100);

console.log(result1);
console.log(result2);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • Note: this is going to work if the error is less than the target, else, this would round up the number. E.g: `28865.000000000002` -> `28866` – AlexSp3 Sep 15 '21 at 11:05
  • Works for this specific case... But... Supposing the conversion goes the other side of the intended value? – spender Sep 15 '21 at 11:05