Limit the amount of number shown after a decimal place in javascript

后端 未结 6 2047
醉酒成梦
醉酒成梦 2020-12-30 20:35

Hay, i have some floats like these

4.3455
2.768
3.67

and i want to display them like this

4.34
2.76
3.67

6条回答
  •  旧巷少年郎
    2020-12-30 21:01

    Warning! The currently accepted solution fails in some cases, e.g. with 4.27 it wrongly returns 4.26.

    Here is a general solution that works always.

    (Maybe I should put this as a comment, but at the time of this writing, I don't have the required reputation)

提交回复
热议问题