[removed] Round to a number of decimal places, but strip extra zeros

前端 未结 8 2187
鱼传尺愫
鱼传尺愫 2020-12-02 12:00

Here\'s the scenario: I\'m getting .9999999999999999 when I should be getting 1.0.
I can afford to lose a decimal place of precision, so I\'m u

8条回答
  •  鱼传尺愫
    2020-12-02 12:23

    As I understand, you want to remove the trailing zeros in the string that you obtained via toFixed(). This is a pure string operation:

    var x = 1.1230000;
    var y = x.toFixed(15).replace(/0+$/, "");  // ==> 1.123
    

提交回复
热议问题