0

I am trying to convert a var to a 2 decimal point number in Javascript, as follows:

var num1 = 9.7000000000

var newNum1= Math.floor(parseFloat(num1) * 100) / 100;

However, the output turns out to be 9.69.

I appreciate any help or advice.


Edit: Thanks everyone, I have also tried .toFixed(2).

However, I encountered issues when using it with arithmetic functions afterwards:

if (weight < newNum1)

Resolved: By adding a unary plus operator + as follows:

newNum1= +num1.toFixed(2);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
gymcode
  • 4,431
  • 15
  • 72
  • 128
  • `num1` is already a floating point number so `parseFloat` doesn't seem like it's doing anything. – apokryfos Jul 05 '20 at 05:46
  • 2
    use `.toFixed(2)` like `num1.toFixed(2)` – Karl L Jul 05 '20 at 05:47
  • Also for why `9.7*100 = 969.9999999999` you can refer to https://stackoverflow.com/questions/588004/is-floating-point-math-broken – apokryfos Jul 05 '20 at 05:49
  • Does this answer your question? [JavaScript displaying a float to 2 decimal places](https://stackoverflow.com/questions/3163070/javascript-displaying-a-float-to-2-decimal-places) – Always Helping Jul 05 '20 at 05:53
  • Is there a way to convert the value after`.toFixed(2)` to make it usable for arithmetic functions such as more than or less than? – gymcode Jul 05 '20 at 05:57
  • This is an odd requirement. Getting two decimal points out of a number is just for presentation because a floating point 9.7000000000 is equal to 9.7 (the same way 0001 is equal to 1). If your actual requirement is to round your number to 2 decimal points (e.g. if you have something like 9.695... and want to get 9.70) that kind of changes the question – apokryfos Jul 05 '20 at 06:07

5 Answers5

2

You can use .toFixed(2) and the unary plus operator to convert the string back to a number.

const res = +num1.toFixed(2);//9.7
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Thanks! I have tried `.toFixed(2)`, but I encountered issue when using for arithmetic functions afterwards, e.g. more than or less than the value – gymcode Jul 05 '20 at 05:58
  • 2
    Did you use the unary plus operator like I have? – Unmitigated Jul 05 '20 at 06:00
  • This is the shortest trick to do the job. But is twice as slow because it involves type conversion from number to string then back to a number. – Mohsen Alyafei Jul 05 '20 at 06:16
2

Have you tried toFixed ?

The toFixed() method converts a number into a string, rounding to a specified number of decimals.

var num1 = 9.7000000000

var newNum1= num1.toFixed(2)

console.log(newNum1)
xMayank
  • 1,875
  • 2
  • 5
  • 19
2

You can use toFixed() method...,

var num1 = 9.7000000000

var newNum1 = num1.toFixed(2);

console.log(newNum1);
Always Helping
  • 14,316
  • 4
  • 13
  • 29
Santa
  • 367
  • 2
  • 8
  • Thanks! I have tried `.toFixed(2)`, but I encountered issue when using for arithmetic functions afterwards, e.g. more than or less than the value – gymcode Jul 05 '20 at 05:58
1

You can use this code snippet:

const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);

console.log(round(9.7000000000, 2));

Ref: https://www.30secondsofcode.org/js/s/round

Anuj Shah
  • 537
  • 4
  • 11
1

If you want to "actually" modify the number to a 2 decimals (not only display formatting) then use the following solution. Here the number is actually changed internally to a new number.

If you use the number.toFixed() method you only show/display the number as a 2 decimal but the number is not changed. toFixed() is used for formatting.

// Round to the required number of desimal places
// @input {number}   number to round
//        {decimals} number of decimal places
// @return {float} rounded number

function numberRoundDecimal(num, decimals) {
return Math.round(num*Math.pow(10,decimals))/Math.pow(10,decimals)
}

// ------- tests --------
console.log(numberRoundDecimal(9.7000000000,2))           // 9.7
console.log(numberRoundDecimal(-0.024641163062896567,3))  // -0.025
console.log(numberRoundDecimal(0.9993360575508052,3))     // 0.999
console.log(numberRoundDecimal(1.0020739645577939,3))     // 1.002
console.log(numberRoundDecimal(0.999,0))                  // 1
console.log(numberRoundDecimal(0.975,0))                  // 1
console.log(numberRoundDecimal(0.975,1))                  // 1
console.log(numberRoundDecimal(0.975,2))                  // 0.98
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42