簡體   English   中英

如何使用不同的十進制符號將數字格式化為貨幣

[英]How can I format a number to curreny with differend decimal signs

我無法將數字格式化為具有不同小數的貨幣?

例如:

// 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更多詳細信息,請參閱頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM