将字符串转换为数字在JavaScript中不起作用。

huangapple go评论76阅读模式
英文:

Converting string to number not working in Javascript

问题

我感到困惑。

$("#total-kwh-cost").text() 包含文本 $920。它先前已使用 toLocaleString() 格式化为 USD 货币 并存储在具有 id#total-kwh-costdiv 中。

我使用以下方式从 div 中检索文本:

var tkwhCostp = $("#total-kwh-cost").text()。replace(/,/g,''); // 作为文本返回$920

然后我检查它以确保它是一个数字

console.log("tkwhCostp 文本是:" + tkwhCostp); // 返回NaN
console.log("tkwhCostp 是NAN吗?" + isNaN(tkwhCostp)); // 返回true
tkwhCostp = parseFloat(tkwhCostp); // 通过parseFloat运行它
console.log("tkwhCostp 文本是:" + tkwhCostp); // 返回NaN
console.log("tkwhCostp 是NAN吗?" + isNaN(tkwhCostp)); // 返回true

我尝试使用 parseInt()Number() 并获得相同的结果。这必须是与我的代码相关的问题。

似乎只影响我使用 toLocaleString() 转换的变量。

我不知道如何将其转换回数字,以便我可以将其添加到数字920中。

英文:

I'm confused.

$("#total-kwh-cost").text() contains the text $920. It has been previously calculated and formatted using toLocaleString() as USD currency and stored in a div with an id of #total-kwh-cost.

I retrieve the text from the div using:

var tkwhCostp = $("#total-kwh-cost").text().replace(/,/g,''); // returns $920 as text

Then I check it to make sure it's been a number

console.log("tkwhCostp text is: "+tkwhCostp);       // return NaN
console.log("tkwhCostp isNAN? "+isNaN(tkwhCostp));  // returns true
tkwhCostp = parseFloat(tkwhCostp);                  // Run it through parseFloat
console.log("tkwhCostp text is: "+tkwhCostp);       // returns NaN  
console.log("tkwhCostp isNAN? "+isNaN(tkwhCostp));  // returns true

I tried parseInt() and Number() and get the same results. This has to be local to my code.

It also appears to only affect variables that I converted using toLocaleString().

I'm at a loss as to how to convert it back to a number so I can add to the number 920.

答案1

得分: 1

JavaScript将美元符号$解释为非数字字符,整个解析操作失败,返回NaN(不是数字)。

要移除逗号和美元符号:

var tkwhCostp = $("#total-kwh-cost").text().replace(/,/g,'').replace(/$/g, ''); // 作为文本返回920
英文:

Javascript interprets the $ sign as non-numeric and the entire parsing operation fails, returning NaN (not a number).

To remove the comma and the dollar sign:

var tkwhCostp = $("#total-kwh-cost").text().replace(/,/g,'').replace(/$/g, ''); // returns 920 as text

答案2

得分: 1

已经使用toLocaleString()进行了先前的计算和格式化,以便在HTML中更好地显示,这是可以的。但为什么然后将其存储在div中以供稍后由JS检索?您已经在JS中有原始的、未格式化的值,用于您的计算,最好使用它。

let originalValue = 920;
let formatedValue = originalValue.toLocaleString("en-US", {style:"currency", currency:"USD"});
$("#total-kwh-cost").text(formatedValue); // 在HTML中使用格式化后的值进行显示
let calculatedValue = originalValue * 0.25; // 在数学计算中使用原始值

您收到NaN是因为toLocaleString()将值更改为无法自动转换为数字的字符串。parseInt()和Number()函数不能将任何内容转换为数字。如果字符串的第一个字符不是数字,例如$,则会返回NaN(非数字)。

英文:

It has been previously calculated and formatted using toLocaleString() for better display in HTML which is fine. But why is it then stored in the div to be retrieved later on by JS? You already have the original, unformatted value in JS for your calculations, it's better to use that instead.

let originalValue = 920;
let formatedValue = originalValue.toLocaleString("en-US", {style:"currency", currency:"USD"});
$("#total-kwh-cost").text(formatedValue); // use the formatted value in HTML for display
let calculatedValue = originalvalue * 0.25; // use the original value for math calculations

You are getting NaN because tolocaleString() changes the value into a string that cannot be automatically converted to a Number. The parseInt() and Number() functions cannot convert anything into a number. If the first character of a string is not a digit e.g. $, then NaN (Not-a-Number) is returned.

huangapple
  • 本文由 发表于 2023年6月2日 10:42:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定