将货币字符串转换为小数?

时间:2013-01-04 17:58:58

标签: c# .net type-conversion

目的

在一组数据中对以string数字显示货币数据的$1,995.94进行排序。

代码

我目前正在使用以下代码示例将string值转换为decimal,以便我可以正确排序。

if (sortBy == "checkAmount")
{
    StringBuilder sb = new StringBuilder();
    foreach (var c in Convert.ToString(p.GetType().GetProperty(sortBy).GetValue(p, null)))
    {
        if (!char.IsDigit(c) && c != '.') { continue; }
        sb.Append(c);
    }
    return Convert.ToDecimal(sb.ToString());
}
else
{
    return p.GetType().GetProperty(sortBy).GetValue(p, null);
}

问题

这样做有什么好办法?它很有用,而且很酷,但它不是很优雅。

最终解决方案

Servy 提供的答案按预期工作,我使用了一段时间的实现,但是我找到了一个更好的方式,所以我在这里记录它。 BTW,我最终最终使用了这个解决方案。

decimal.Parse(input, NumberStyles.AllowCurrencySymbol | NumberStyles.Number);

6 个答案:

答案 0 :(得分:25)

这个怎么样,但只适用于一个字符串值。因此,您需要在split之前获取字符串$,然后在保存到arraylist

的同时进行转换
 using System.Globalization;
    //rest of your code

          string str = "$50,550.20";
          decimal decval;
          bool convt = decimal.TryParse(str, NumberStyles.Currency,
            CultureInfo.CurrentCulture.NumberFormat, out decval);
          if (convt) 
          Console.WriteLine(decval);
          Console.ReadLine();

答案 1 :(得分:12)

这是一种与您提供的代码非常相似的方法

public static decimal Parse(string input)
{
    return decimal.Parse(Regex.Replace(input, @"[^\d.]", ""));
}

这是一个支持负数的选项,如果找到第二个句点值,它将停止,从而减少它返回的非有效decimal值的字符串数。它还有一些在OP中没有看到的其他修改,以处理当前代码没有的其他情况。

public static decimal Parse(string input)
{
    return decimal.Parse(Regex.Match(input, @"-?\d{1,3}(,\d{3})*(\.\d+)?").Value);
}

答案 2 :(得分:8)

decimal amount = decimal.Parse("$123,456.78",
NumberStyles.AllowCurrencySymbol |
NumberStyles.AllowThousands |
NumberStyles.AllowDecimalPoint);

答案 3 :(得分:7)

这是一个更简单的解决方案:

    public static decimal ToDecimal(this string str)
    {
        return decimal.Parse(str, NumberStyles.Currency);
    }

和单元测试:

    [Test]
    public void ToDecimal_Convert_String_To_Decimal()
    {
        Assert.AreEqual(1234M, "1234".ToDecimal());
        Assert.AreEqual(-1234.56M, "$(1,234.56)".ToDecimal());
        Assert.AreEqual(1234.56M, "$1,234.56".ToDecimal());
    }

答案 4 :(得分:0)

public static decimal ToDecimalFromStringDecimalOrMoneyFormattedDecimal(this string s)
{
    try
    {
        return decimal.Parse(s);
    }
    catch
    {
        var numberWithoutMoneyFormatting = Regex.Replace(s, @"[^\d.-]", "");
        return decimal.Parse(numberWithoutMoneyFormatting);
    }
}

[Test]
public void Test_ToDecimalFromStringDecimalOrMoneyFormattedDecimal()
{
    Assert.That("$ 500".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)500);
    Assert.That("R -500".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)-500);
    Assert.That("-$ 500".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)-500);
    Assert.That("P 500.90".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)500.9);
    Assert.That("$ -50 0,090,08.08".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)-50009008.08);
}

答案 5 :(得分:0)

适用于所有文化:

var d = decimal.Parse("$497.7", NumberStyles.Currency, CultureInfo.CreateSpecificCulture("us-US").NumberFormat);    

Console.WriteLine(d);
相关问题