Javascript jquery-toFixed但不是toFixed

Javascript jquery-toFixed但不是toFixed,javascript,integer,decimal,Javascript,Integer,Decimal,使用toFixed如下所示: var a=0.5, b=1, c=1.5; console.log(a.toFixed(), b.toFixed(), c.toFixed()); // 0.5 1.0 1.5 然而,当它是一个整数时,我只希望它返回“1” 救命啊 可以使用正则表达式删除尾部的.0,如果它存在: Number.prototype.safe_toFixed = function (x) { var that = this.toFixed(x); return tha

使用toFixed如下所示:

var a=0.5, b=1, c=1.5;
console.log(a.toFixed(), b.toFixed(), c.toFixed());
// 0.5 1.0 1.5
然而,当它是一个整数时,我只希望它返回“1”


救命啊

可以使用正则表达式删除尾部的
.0
,如果它存在:

Number.prototype.safe_toFixed = function (x) {
    var that = this.toFixed(x);
    return that.replace(/\.0$/, '');
}
您可以使用
split()

    var digit = 1.2
    var ret = digit.toFixed(1);
    var intValue = ret.split('.');
    if(intValue[1] == 0){
      digit = intValue[0];
    }

这就是我所做的,每次都有效

var x= Number(54.03).toFixed(1);

  if(Math.floor(x) == x) {
     x = Math.floor(x);
  }

alert( x );

我只是比较这两种类型,看看它们是否匹配。如果他们这样做了,那么我知道可能有,也可能没有额外的零。不管是哪种方式,我只需向上(ceil)或向下(floor)取整,就可以得到一个没有令人讨厌的小数点和尾随零的整数。

这个问题实际上与jquery无关。toFixed内置于javascript中。我打开此页面是希望看到莎士比亚。你知道你在搞乱
数字
原型吗?这可能会影响页面上的其他脚本@科迪,不管是谁受到了侮辱,我认为这应该得到一面旗帜:)@belisarius:看,这正是我心中的问题。如果你只是在冒犯自己,这真的是“冒犯”吗?然后我的头爆炸了,因为我一开始就想弄明白它。@Cody哦,我明白了。你认为这是自杀,该不该被判死刑。我只是看到一面旗帜。也许我今天的生物节律不太适合诗歌:)