Converting number into string in JavaScripts without trailing zeros from number

前端 未结 2 1519
星月不相逢
星月不相逢 2021-01-26 11:53

I tried to convert number to string in JavaScripts using toString() but it truncates insignificant zeros from numbers. For examples;

var n1 = 250.00 
var n2 = 59         


        
2条回答
  •  鱼传尺愫
    2021-01-26 12:21

    As far as I know, you can't store number with floating zeros, but you can create zeroes with floating zeroes, by using toFixed:

    var n1 = 250;
    var floatedN1 = n1.toFixed(2); //type 'string' value '250.00'
    

提交回复
热议问题