如何将数字格式化为具有不同小数符号的 curreny



我不能将数字格式化为小数不同的货币吗?

例如:

// for numbers I can use the placeholder #
decimal d1 = 1.2345;
decimal d2 = 1.23;
String.Format("{0:0.####}", d1); //results in 1.2345
String.Format("{0:0.####}", d2); //results in 1.23
// with the format specifier C I cannot use the placeholder
String.Format("{0:C4}", d1); //results in 1.2345 €
String.Format("{0:C4}", d2); //results in 1.2300 €
// I need something like this
String.Format("{0:C####}", d1); //results in 1.2345 €
String.Format("{0:C####}", d2); //results in 1.23 €
// I don't want to use this solution because I use my program in different countries
String.Format("{0:0.#### €}", d1); //results in 1.2345 €
String.Format("{0:0.#### €}", d2); //results in 1.23 €

有人有想法吗?

谢谢!

这是你的答案:

double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

如果你想要超过 2 个十进制数字(假设小数点后 3 位(,那么它将是

double value = 12345.6789;
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

这是假设您的应用程序将在不同的区域性下执行。 这就是为什么我们使用currentCulture.

否则,您可以根据要使用的区域性创建CultureInfo实例。

有关数字格式,请参阅此文档,有关CultureInfo的更多详细信息,请参阅此页面。

最新更新