将十进制货币转换为逗号分隔的值 [英] Convert decimal currency to comma separated value

查看:62
本文介绍了将十进制货币转换为逗号分隔的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Web服务获得以下字符串:

I get from a webservice the following strings:

12.95

1,200.99

是否可以在不处理字符串的情况下将这些值转换为以下值?

Is there an option to convert these values to the following values without manipulating the string?

12,95

1200,99

我尝试了一些文化选项,但并没有正确...

I tried it with some Culture options but didn't get it right...

我尝试过:

    //return string.Format( "{0:f2}", Convert.ToDecimal( price ) );
    //return string.Format(CultureInfo.GetCultureInfo("de-de"), "{0:0}", price);

                

    NumberFormatInfo format = new System.Globalization.NumberFormatInfo();

    format.CurrencyDecimalDigits = 2;
    format.CurrencyDecimalSeparator = ",";
    format.CurrencyGroupSeparator = "";

    return decimal.Parse(price).ToString(format);

推荐答案

var input = "1,200.99";

//Convert to decimal using US culture (or other culture using . as decimal separator)
decimal value = decimal.Parse(input, CultureInfo.GetCultureInfo("en-US"));

//Convert to string using DE culture (or other culture using , as decimal separator)
string output = value.ToString(CultureInfo.GetCultureInfo("de-DE"));

Console.WriteLine(output); //1200,99

这篇关于将十进制货币转换为逗号分隔的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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