是否可以 return 一个带 2 个小数位的浮点值表示 0?
Is it possible to return a float value with 2 decimal places for 0?
我正在推动将价格输入 Google 分析和价格 return 作为浮动价格的指导方针之一,到目前为止一切顺利。我使用 parseFloat () 解决了问题,但是,对于以 00 结尾的值,我必须 return 两位小数。示例:(100.00 或 208.00)。当它 return 是一个浮点值时,它会删除以 0 结尾的值。
var product_price = “100.00”;
值 = parseFloat(product_price);
浮动结果= 100
我使用 .toFixed (2) 方法来放置小数点后两位,但 return 作为字符串。
好吧,我已经在几个地方进行了研究,我找到的所有解决方案和我知道的解决方案都将 return 一个字符串。我认为从技术上讲这是不可能的。但我想确定这真的不可能。
Saberiam me dizer se é possivel retornar um valor com duas casas decimais para valores de final .00。
澄清 parseFloat return 浮点数为 100.99 的值,现在不是 100.00。
抱歉之前用葡萄牙语写问题。
谢谢
数值本身没有小数位的概念,除非你想打印它。如果您不打印出来,数字类型具有固定的小数位就没有任何意义。因此,只有当您将数字转换为 String
.
时,小数位才会起作用
每当你想 "see" 100.00
时,你已经意味着 convert the number with value = 100 to a String with 2 decimal places
"see"。您只能看到 String
而看不到数字。
您混淆了两件事:数字的值及其显示方式。
100、100.0、100.00(甚至 1E2、10E1、0.1E3)是相同 数值.
的视觉表示
我将抛开计算机数值并不总是与数学意义上的实数相同的事实。
在JavaScript、parseFloat
、returns一个numeric
类型。 numeric
值 默认显示 没有尾随零。也就是说,当您执行 console.log(100)
时,100 被转换为字符串“100”,而 console.log(100.00)
也是如此,数字也被转换为字符串...“100” .
但是您始终可以通过强制显示小数位数来指定字符串表示形式(这就是 .toFixed()
所做的)。
参见:Format number to always show 2 decimal places
您可以简单地在任何 numeric
绑定(变量)上使用 toFixed(2)
来实现您想要的。
let a = 100;
console.log(a.toFixed(2));
I used the .toFixed (2) method to place the two decimal places but return as String.
是的,但实际上您尝试 显示 的任何数字在某些时候都是某种字符串(想想 console.log(100.00)
)。 100 和 100.00 的内部值 (numeric
) 相同。
仅的区别在于它们的字符串表示,因此要求一种方法来做到这一点"without string"是矛盾的。
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function sayHello() {
var num = "100.00";
var value=parseFloat(num);
var n = value.toFixed(2);
alert(n);
}
sayHello();
</script>
</head>
<body>
</body>
</html>
我正在推动将价格输入 Google 分析和价格 return 作为浮动价格的指导方针之一,到目前为止一切顺利。我使用 parseFloat () 解决了问题,但是,对于以 00 结尾的值,我必须 return 两位小数。示例:(100.00 或 208.00)。当它 return 是一个浮点值时,它会删除以 0 结尾的值。
var product_price = “100.00”; 值 = parseFloat(product_price);
浮动结果= 100
我使用 .toFixed (2) 方法来放置小数点后两位,但 return 作为字符串。
好吧,我已经在几个地方进行了研究,我找到的所有解决方案和我知道的解决方案都将 return 一个字符串。我认为从技术上讲这是不可能的。但我想确定这真的不可能。
Saberiam me dizer se é possivel retornar um valor com duas casas decimais para valores de final .00。
澄清 parseFloat return 浮点数为 100.99 的值,现在不是 100.00。
抱歉之前用葡萄牙语写问题。
谢谢
数值本身没有小数位的概念,除非你想打印它。如果您不打印出来,数字类型具有固定的小数位就没有任何意义。因此,只有当您将数字转换为 String
.
每当你想 "see" 100.00
时,你已经意味着 convert the number with value = 100 to a String with 2 decimal places
"see"。您只能看到 String
而看不到数字。
您混淆了两件事:数字的值及其显示方式。
100、100.0、100.00(甚至 1E2、10E1、0.1E3)是相同 数值.
的视觉表示我将抛开计算机数值并不总是与数学意义上的实数相同的事实。
在JavaScript、parseFloat
、returns一个numeric
类型。 numeric
值 默认显示 没有尾随零。也就是说,当您执行 console.log(100)
时,100 被转换为字符串“100”,而 console.log(100.00)
也是如此,数字也被转换为字符串...“100” .
但是您始终可以通过强制显示小数位数来指定字符串表示形式(这就是 .toFixed()
所做的)。
参见:Format number to always show 2 decimal places
您可以简单地在任何 numeric
绑定(变量)上使用 toFixed(2)
来实现您想要的。
let a = 100;
console.log(a.toFixed(2));
I used the .toFixed (2) method to place the two decimal places but return as String.
是的,但实际上您尝试 显示 的任何数字在某些时候都是某种字符串(想想 console.log(100.00)
)。 100 和 100.00 的内部值 (numeric
) 相同。
仅的区别在于它们的字符串表示,因此要求一种方法来做到这一点"without string"是矛盾的。
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function sayHello() {
var num = "100.00";
var value=parseFloat(num);
var n = value.toFixed(2);
alert(n);
}
sayHello();
</script>
</head>
<body>
</body>
</html>