[英]Convert localized string into decimal in javascript
I have a decimal, that could look like 123,34
, 123123,09
, 1234
, 123.34
, 123123.09
. 我有一个小数,可能看起来像
123,34
, 123123,09
, 1234
, 123.34
, 123123.09
。
My MVC application supports localization, I need to find a safe way to convert the most likely user input into a decimal. 我的MVC应用程序支持本地化,我需要找到一种将最可能的用户输入转换为十进制的安全方法。 So for decimal value
.
因此对于十进制值
.
will represent as ,
(eg 12.34 => 12,34) 将表示为
,
(例如12.34 => 12,34)
I want to get this value in javascript as 我想在javascript中获取此值
var maxAmount = parseFloat("@Model.MaxAmount");
if MaxAmount is 1,25
I will get a result as 1
only. 如果MaxAmount为
1,25
我将得到的结果仅为1
。
How is that possible? 那怎么可能?
为什么不使用MaxAmount.ToString(CultureInfo.InvariantCulture)指定模型属性的格式-这样,十进制始终将序列化为1.25,并且仍可以由JavaScript正确解释。
Just do the replace for the edge case of xxx,xx
: 只需替换
xxx,xx
的边缘情况:
var maxAmount = parseFloat('@Model.MaxAmount'.replace(',', '.'));
This will convert string of type "123,45"
to "123.45"
but keep the "123.45"
in the same format. 这会将类型
"123,45"
字符串转换为"123.45"
但将"123.45"
保留为相同格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.