2

I'm using react to control the value. Is the option of rounding the value to two decimal places in HTML input type number? I have step 0.166666666, but in HTML input view I'd like to display: 0.17 instead of 0.166666666666, 0.33 instead of 0.333333333333 and so on...

I do not want to change the value of input, I would like to change only the Input view not value. toFixed won't work for me.

Piotr Białek
  • 2,569
  • 1
  • 17
  • 26
  • Possible duplicate of [Formatting a number with exactly two decimals in JavaScript](https://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) – Pierre C. Apr 03 '18 at 12:01

3 Answers3

1

You need to use toFixed(2) function.

0.166666666666.toFixed(2);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33
1

You can use toFixed(2); function of javascript. For ex:-

var num = 5.56789;
var n = num.toFixed(2);
Rupal
  • 1,111
  • 6
  • 16
1

Use toFixed(2) to round it.

This will round 0.166666666666 to 0.17, and 0.333333333333 to 0.33.

If for example, you need 0.16 to be 0.166666666666 in your logic, only use toFixed(2) for the part of your code that will update what's visible in the input.

Ibrahim
  • 332
  • 3
  • 14