值“1.500,10”对价格无效 [英] The value ' 1.500, 10 ' is not valid for price

查看:68
本文介绍了值“1.500,10”对价格无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络配置



Web Config

<globalization uiCulture="en-US" culture="en-US" />





实体





Entity

[DisplayName("Price")]
public double Price { get; set; }





Create.cshtml





Create.cshtml

<div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
        </div>
    </div>





我尝试过:



问题



我试图用正则表达式弄明白,但我不能。



我在添加产品价格时遇到此错误。我该如何解决?谢谢。



What I have tried:

Problem

I tried to figure it out using regular expression, but I couldn't.

I get this error when I add the product price.How can I fix? Thank you.

推荐答案

告诉用户输入没有千位分隔符的值,或者在尝试将数字字符串转换为数字之前将其删除。



另一个问题是 parseFloat()只接受周期为小数点的数字字符串。因此可能需要替换逗号等其他小数点。



因为这两个问题都是相关的,所以支持带有数千个分隔符的输入字符串是非常困难的。



简单的解决方案(输入必须没有千位分隔符)

Tell the user to enter values without thousands separator or remove it before trying to convert numeric strings to numbers.

Another problem is that parseFloat() accepts only numeric strings with the period as decimal point. So it might be necessary to replace other decimal points like the comma.

Because both problems are related, supporting input strings with thousands separators is quite difficult.

The simple solution (input must be without thousands separator)
priceEN = price.replace(/[,\u2396\u066B]/g, '.');
priceValue = parseFloat(priceEN);

这将用句号替换其他小数点。



高级解决方案(需要定义或确定语言环境)

从语言环境中获取千位分隔符并将其从字符串中删除。期间的示例(使用逗号作为小数点的区域设置):

This will replace other decimal points with the period.

The advanced solution (requires defining or determining a locale)
Get the thousands separator from the locale and remove it from the string. Example for the period (locales using comma as decimal point):

priceLoc = price.replace(/[^0-9-,]/g, ''));

对于使用句点作为小数点的语言环境

For locales using period as decimal point

priceLoc = price.replace(/[^0-9-.]/g, ''));

以上内容还将删除货币符号等其他字符。如果小数点不是句点,则在调用 parseFloat()之前,将结果字符串从简单示例传递给正则表达式。

The above will also remove other characters like currency symbols. If the decimal point is not the period, pass the resulting string to the regex from the simple example before calling parseFloat().


这篇关于值“1.500,10”对价格无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆