Javascript 将货币格式转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8654456/
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-24 06:56:21  来源:igfitidea点击:

convert money format into numbers

javascriptjquery

提问by user818671

i have the following tag in html i do not want the $ to be taken as i just want the price for calculations purpose.

我在 html 中有以下标签,我不希望使用 $,因为我只想要价格用于计算目的。

<span id="testspan">1.82</span>

in the above span tag i want to take only 101.82 value for calculations .

在上面的跨度标签中,我只想取 101.82 值进行计算。

I am using the html() to get the value

我正在使用 html() 来获取值

var csqft_price = $('#testspan').html();
var price= parseFloat(csqft_price);

but i am getting it with $ so i am unable to do calculation how can i do it.

但我用 $ 得到它,所以我无法计算我该怎么做。

回答by Hemant Metalia

You can use

您可以使用

var csqft_price = $('#testspan').html();
var number = Number(csqft_price.replace(/[^0-9\.]+/g, ""));

This was already posted on SO here.

这已经张贴在 SO here 上

回答by nnnnnn

The following tests whether the first character is a dollar-sign and if so takes the rest of the string:

以下测试第一个字符是否是美元符号,如果是,则使用字符串的其余部分:

var csqft_price = $('#testspan').html();
var price = +(csqft_price.charAt(0)==="$" ? csqft_price.substr(1) : csqft_price);

Of course, if you know that there will alwaysbe a dollar-sign you can just do this:

当然,如果你知道总会有一个美元符号,你可以这样做:

var price = +csqft_price.substr(1);

Note: I generally prefer the unary plus operatorto convert a string to a number, so I've used that in my answer above - you can change +(...)to parseFloat(...)if you prefer.

注意:我通常更喜欢使用一元加号运算符将字符串转换为数字,因此我在上面的回答中使用了它 -如果您愿意,可以更改+(...)parseFloat(...)

回答by Armin

I would replace all non digets and dots with nothing and make than a parseFloat:

我会用空替换所有非数字和点,并生成一个 parseFloat:

var textValue = '1.82';
var floatedValue = parseFloat(textValue.replace(/[^\d\.]/, ''));
alert(floatedValue);

Example: http://jsfiddle.net/MEY9R/

示例:http: //jsfiddle.net/MEY9R/

回答by Vikas Naranje

Try this -

尝试这个 -

   var csqft_price = $('#testspan').text();
    var s = csqft_price.substr(1);
    alert(s);

http://www.w3schools.com/jsref/jsref_substr.asp

http://www.w3schools.com/jsref/jsref_substr.asp

回答by Rajesh Kumar Maurya

Use this

用这个

var csqft_price = $('#testspan').html();
var price=parseFloat(csqft_price.replace('$',''));
alert(price);

And use price for calculation.

并使用价格进行计算。

回答by user3692854

ku4js-kernel has a sweet $.money class for this. You can just $.money.parse("$101.82") and then do what you want.

ku4js-kernel 有一个很好的 $.money 类。你可以只是 $.money.parse("$101.82") 然后做你想做的。

You could get the value() and use as a number or even better, just keep as money and do your operations. You can check out the documentation here.

您可以获取 value() 并用作数字,甚至更好,只需保留为钱并进行操作即可。您可以在此处查看文档。

回答by Oldskool

Try replacing the $ sign by using the javascript "replace" function and replace the $ sign with an empty value, like this:

尝试使用 javascript“替换”函数替换 $ 符号并将 $ 符号替换为空值,如下所示:

var vsqft_price = $('#testspan').html();
var vsqft_float = vsqft_price.replace("$", "");
var price= parseFloat(csqft_float);