How do you round to 1 decimal place in Javascript?

前端 未结 21 1862
难免孤独
难免孤独 2020-11-22 08:49

Can you round a number in javascript to 1 character after the decimal point (properly rounded)?

I tried the *10, round, /10 but it leaves two decimals at the end of

21条回答
  •  旧巷少年郎
    2020-11-22 08:53

    Math.round( num * 10) / 10 doesn't work.

    For example, 1455581777.8-145558160.4 gives you 1310023617.3999999.

    So only use num.toFixed(1)

提交回复
热议问题