从数字中删除无关紧要的尾随零? [英] Remove insignificant trailing zeros from a number?

查看:164
本文介绍了从数字中删除无关紧要的尾随零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否错过了一个标准的API调用,可以从一个数字中删除尾随无效的零?

Have I missed a standard API call that removes trailing insignificant zeros from a number?

前。

var x = 1.234000 // to become 1.234;
var y = 1.234001; // stays 1.234001

Number.toFixed()和Number.toPrecision()不是我的意思正在寻找。

Number.toFixed() and Number.toPrecision() are not quite what I'm looking for.

推荐答案

如果将其转换为字符串,则不会显示任何尾随的零,这些零都没有存储在自从它创建为数字而不是字符串以来的第一个变量。

If you convert it to a string it will not display any trailing zeros, which aren't stored in the variable in the first place since it was created as a Number, not a String.

var n = 1.245000
var noZeroes = n.toString() // "1.245" 

这篇关于从数字中删除无关紧要的尾随零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆