【问题标题】:JQuery - Change integer to float with one decimal pointJQuery - 将整数更改为带一位小数的浮点数
【发布时间】:2019-02-13 07:16:56
【问题描述】:

例如。如果我给 150 //输出应该是 15.0 if 1565 //输出应该是156.5

谁能提供上述逻辑的最佳代码?

【问题讨论】:

    标签: javascript jquery floating-point numbers


    【解决方案1】:

    使用.parseFloat().toFixed(1) 将精度设置为1。 toFixed() 中提供的值将设置精度直到该值

    var a = 150;
    console.log(parseFloat(a / 10).toFixed(1))
    
    a = 1565;
    console.log(parseFloat(parseFloat(a / 10).toFixed(1))) //to get the output type as number

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 2015-05-20
      • 2016-10-19
      • 2012-08-16
      • 2011-10-25
      相关资源
      最近更新 更多