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