如何在 JavaScript 中将整数转换为十进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2048101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:33:49  来源:igfitidea点击:

How do I convert an integer to decimal in JavaScript?

javascript

提问by jon colins

I have a number in JavaScript that I'd like to convert to a money format:

我在 JavaScript 中有一个数字,我想将其转换为货币格式:

556633 -> £5566.33

How do I do this in JavaScript?

我如何在 JavaScript 中做到这一点?

回答by knipknap

Try this:

尝试这个:

var num = 10;
var result = num.toFixed(2); // result will equal 10.00

回答by Rob Levine

This works:

这有效:

var currencyString = "£" + (amount/100).toFixed(2);

回答by YOU

Try

尝试

"£"+556633/100

回答by Guven

This script making only integer to decimal. Seperate the thousands

这个脚本只制作整数到十进制。分开千

onclick='alert(MakeDecimal(123456789));' 


function MakeDecimal(Number) {
        Number = Number + "" // Convert Number to string if not
        Number = Number.split('').reverse().join(''); //Reverse string
        var Result = "";
        for (i = 0; i <= Number.length; i += 3) {
            Result = Result + Number.substring(i, i + 3) + ".";
        }
        Result = Result.split('').reverse().join(''); //Reverse again
        if (!isFinite(Result.substring(0, 1))) Result = Result.substring(1, Result.length); // Remove first dot, if have.
        if (!isFinite(Result.substring(0, 1))) Result = Result.substring(1, Result.length); // Remove first dot, if have.
        return Result;

    }