如何输出数字2.00作为完整的字符串?

Sla*_*djo 1 javascript floating-point

在此之前,我想说对不起.但这不重复.其他帖子的任何答案都有同样的问题.JS中没有float或int(仅限数字).当您执行isInt()功能时,2.00始终检测true为整数.我希望2.00被检测为浮动.所以,我必须首先对其进行字符串化.

function isInt(i) {
    if ( i.toFixed ) {
        if ( i % 1 === 0 ) {
            return true; // the problem is 2.00 always detected true as integer. 
            // i want 2.00 detected as float
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我想我将字符串化2.00然后用split('.')拆分它.但toString没有这样做

var i = 2.00;
alert(i.toString()); 
// Why this always result 2 . i want the character behind point
Run Code Online (Sandbox Code Playgroud)

那么,该怎么做?我想要2.00结果"2.00",而不仅仅是"2"

谢谢你的回答

and*_*lrc 7

您可以使用 Number.toFixed(n);

var i = 2;

alert( i.toFixed(2) ); // "2.00"
Run Code Online (Sandbox Code Playgroud)
var i = 1.2345;

alert( i.toFixed(2) ); // "1.23"
Run Code Online (Sandbox Code Playgroud)

还要注意2 === 2.00但是2 !== "2.00".