关于js数字转换字符串的一个问题

比如:

var num  = 0.00;//如何在知道变量num含有小数点的情况下,转换为字符串保留后2位,而不是通过Fixed(2)
                //强行保留2位小数。
                //尝试过   num+""  ,也不行,返回一个number类型0

可能我的意思是,如果变量是0.0000,这样的数字型的变量也可以自动转换成"0.0000",这样的话,就不能把Fixed()中的参数写死,而且也不能根据字符串加".0000",其实核心问题就是把浮点数最后位全部为0000的保留下来为字符串。。。有点奇葩。。

如果想通过Fixed(num的长度),这种也是不行的,因为通过字符串话的num,已经失去了小数位和小数的长度了,,,,,,

阅读 7.3k
8 个回答

(Math.floor(2.33333*100)/100).toString() 这样吗?

function demo (num) {    
    num = (num.toString()).match(/(\d+\.\d{0,2})|(\d+)/)[0];    
    if(!/\./.test(num)) {
        num = num + '.00';
    }else if(/\.\d{1}$/.test(num)){
        num = num + '0';
    }    
    return num;
}

console.log(demo(6))      //6.00
console.log(demo(6.0))    //6.00
console.log(demo(6.1))    //6.10
console.log(demo(6.21))   //6.21
console.log(demo(6.333))  //6.33
console.log(demo(0))      //0.00
console.log(demo(0.1))    //0.10
console.log(demo(0.01))   //0.01
console.log(demo(0.001))  //0.00

Number(Number(num).toFixed(2))

你的意思是当有小数点的时候保留2位,没有的时候就不要显示有小数点?如果是这样的话可以试试这个:

var a = 9;
var b = 9.258;

function demo(num) {
    var reg = /\./g; //查看是否存在小数点
    if(reg.test(num)) { //若存在,则保留两位小数,倒是不用考虑是否用toFixed,因为这个挺好用的
        return num.toFixed(2);
    } else {//不存在,返回原值
        return num;
    }
}
console.log(demo(a)); //9
console.log(demo(b)); //9.26
新手上路,请多包涵

num.toFixed(2).toString()

新手上路,请多包涵
var a = 0.00
undefined
a
0
var b = 1.00
undefined
b
1

直接在Google浏览器控制台做的测试,结果就是这样,在存储的时候就已经把小数点后的0抹去了
所以我觉得你想在转化成字符串后仍保留两位小数的话,只能手动添加,比如toString之后再+'.00'

新手上路,请多包涵

function d(alan)//小数位相关处理

    {
        var v = Number(alan);
        var r;
        if (alan <= 0)//0或者负数报错
        {
            alert("输入数值必须大于零");
        }
        else if (alan <= 0.9)//0.9以内的纯小数,保留一位有效数字
        {
            var i = 0;
                while (alan <= 0.9)
                {
                    alan=alan*10;
                    i++;
                }
            r = v.toFixed(i);
        }
        else if (alan-Math.round(alan) == 0)//没有小数位,返回整数数值
        {
            r = alan;
        }
        else if (Math.abs(alan-Math.round(alan))<0.1)//小数位可以忽略,返回舍入后的整数值
        {
            r = Math.round(alan);
        }
        else if (Math.abs(alan-Math.round(alan))>=0.1)//小数位不可忽略,保留一位小数
        {
            r = v.toFixed(1);
        }
        return r;
    }

这是我给自己做的计算工具中关于数字处理用的函数,和你不一样,没有转换为字符串,也只保留一位小数,但大体性质是类似的,供参考吧。
倒数第二个的意义是处理1.99舍入后变成2.00这样会产生多余小数位的情况,所以干脆独立做了一个条件筛选。

建议给个示例,不然题干描述有点模糊。
我现在理解类似于补0操作,即不管小数点后面是0还是非0,都保留两位并转换为string.
那这就是一个toFixed(2),几乎一模一样。
那么,只能再实现一次toFixed(),这不算是重新【发明】轮子,这是纯粹地重新【制造】轮子。
既然是重造,那么就必须在原有轮子上作改进,否则没有任何意义。
于是,就有了:参考链接
代码如下:

<html>
    <head>
        <script type="text/javascript">
            Number.prototype.toFixed=function (d) { 
                var s=this+""; 
                if(!d)d=0; 
                if(s.indexOf(".")==-1)s+="."; 
                s+=new Array(d+1).join("0"); 
                if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+(d+1)+"})?)\\d*$").test(s)){
                    var s="0"+RegExp.$2,pm=RegExp.$1,a=RegExp.$3.length,b=true;
                    if(a==d+2){
                        a=s.match(/\d/g); 
                        if(parseInt(a[a.length-1])>4){
                            for(var i=a.length-2;i>=0;i--){
                                a[i]=parseInt(a[i])+1;
                                if(a[i]==10){
                                    a[i]=0;
                                    b=i!=1;
                                }else break;
                            }
                        }
                        s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");
 
                    }if(b)s=s.substr(1); 
                    return (pm+s).replace(/\.$/,"");
                }return this+"";
 
            };
        </script>
    </head>
    <body>
        <input type="button" value="显示0.009.toFixed(2)" onclick="alert(0.009.toFixed(2))"><br />
        <input type="button" value="显示0.123.toFixed(2)" onclick="alert(0.123.toFixed(2))"><br />
        <input type="button" value="显示0.125.toFixed(2)" onclick="alert(0.125.toFixed(2))"><br />
        <input type="button" value="显示0.126.toFixed(2)" onclick="alert(0.126.toFixed(2))"><br />
        <input type="button" value="显示20.445.toFixed(2)" onclick="alert(20.445.toFixed(2))"><br />
        <input onclick="alert(20.405.toFixed(2))" type="button" value="显示20.405.toFixed(2)"> <br />
        <input onclick="alert(20.415.toFixed(2))" type="button" value="显示20.415.toFixed(2)"> <br />
        <input onclick="alert(20.425.toFixed(2))" type="button" value="显示20.425.toFixed(2)"> <br />
        <input onclick="alert(20.435.toFixed(2))" type="button" value="显示20.435.toFixed(2)"> <br />
        <input onclick="alert(20.445.toFixed(2))" type="button" value="显示20.445.toFixed(2)"> <br />
        <input onclick="alert(20.455.toFixed(2))" type="button" value="显示20.455.toFixed(2)"> <br />
        <input onclick="alert(20.465.toFixed(2))" type="button" value="显示20.465.toFixed(2)"> <br />
        <input onclick="alert(20.475.toFixed(2))" type="button" value="显示20.475.toFixed(2)"> <br />
        <input onclick="alert(20.485.toFixed(2))" type="button" value="显示20.485.toFixed(2)"> <br />
        <input onclick="alert(20.495.toFixed(2))" type="button" value="显示20.495.toFixed(2)"> <br />
        <input onclick="alert(0.05.toFixed(1))" type="button" value="显示0.05.toFixed(1)"> <br />
        <input onclick="alert(0.15.toFixed(1))" type="button" value="显示0.15.toFixed(1)"> <br />
        <input onclick="alert(0.25.toFixed(1))" type="button" value="显示0.25.toFixed(1)"> <br />
        <input onclick="alert(0.35.toFixed(1))" type="button" value="显示0.35.toFixed(1)"> <br />
        <input onclick="alert(0.45.toFixed(1))" type="button" value="显示0.45.toFixed(1)"> <br />
        <input onclick="alert(0.55.toFixed(1))" type="button" value="显示0.55.toFixed(1)"> <br />
        <input onclick="alert(0.65.toFixed(1))" type="button" value="显示0.65.toFixed(1)"> <br />
        <input onclick="alert(0.75.toFixed(1))" type="button" value="显示0.75.toFixed(1)"> <br />
        <input onclick="alert(0.85.toFixed(1))" type="button" value="显示0.85.toFixed(1)"> <br />
        <input onclick="alert(0.95.toFixed(1))" type="button" value="显示0.95.toFixed(1)"> <br />
    </body>
</html>

以上是方案一。
方案二就是先将浮点数转换为整型进行精度处理,然后对整形进行处理,处理完毕后进行字符串操作。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题