繁体   English   中英

在asp.net中的c#.net中将多变量从字符串转换为十进制抛出'System.FormatException'类型的异常

[英]convert multi variable from string to decimal in c#.net in asp.net threw an exception of type 'System.FormatException'

我有 2 个或可能超过 2 个字符串变量,如下所示

string a = csvalue[7].Replace(" ", ""); //a="‏120641"
string b = csvalue[9].Replace(" ", "") ;//b="‏"221707‏‏‏‏
decimal result = Convert.ToDecimal(a) + Convert.ToDecimal(b)

但它有例外:“抛出了'System.FormatException'类型的异常”

我试试

       enter code here 
       string one = "‏120641‏‏‏‏";
        string two = "‏221707‏‏‏‏";
        string three = "123548";
       
        Double iOne = 0;
        Double iTwo = 0;
        Double ithree = 0;

        Double.TryParse(one, out iOne);
        Double.TryParse(two, out iTwo);
        Double.TryParse(three, out ithree);

它对 iOne 不起作用,iTwo 请帮助我

有一些(不可见的)unicode 字符不是数字,因此无法解析。 例如,您可以使用 Linq 和char.IsDigit删除它们:

using System;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        string one = "‏120641‏‏‏‏";
        string two = "‏221707‏‏‏‏";
        string three = "123548";
        one = new string(one.Where(char.IsDigit).ToArray());
        two = new string(two.Where(char.IsDigit).ToArray());
        three = new string(three.Where(char.IsDigit).ToArray());
       
        Double iOne = 0;
        Double iTwo = 0;
        Double ithree = 0;

        Double.TryParse(one, out iOne);
        Double.TryParse(two, out iTwo);
        Double.TryParse(three, out ithree);
        Console.WriteLine(iOne);
        Console.WriteLine(iTwo);
        Console.WriteLine(ithree);
    }
}

.Net小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM