C# 使用 decimal.ToString("C") 和 CultureInfo 自定义货币符号和小数位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2316517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:22:11  来源:igfitidea点击:

Custom Currency symbol and decimal places using decimal.ToString("C") and CultureInfo

c#decimalcurrency

提问by Agamand The True

I have a problem with decimal.ToString("C")override. Basically what I wants to do is as follows:

我有decimal.ToString("C")覆盖问题。基本上我想做的如下:

CultureInfo usCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = usCulture;

NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
LocalFormat.CurrencySymbol = "RM";

I wants to make above code a function (override ToString("C")) whereby when the following code get executed:

我想让上面的代码成为一个函数(覆盖 ToString("C")),当下面的代码被执行时:

decimal paid = Convert.ToDecimal(dr["TotalPaids"]);
lblPaids.Text = paid.ToString("C");

The results would be RM4,900.00 instead of $4,900.00

结果将是 RM4,900.00 而不是 $4,900.00

How do I create an override for decimal.ToString("C")that would solve my problem

我如何创建一个覆盖来decimal.ToString("C")解决我的问题

Thanks in advance.

提前致谢。

采纳答案by Mike Two

To get a format like RM 11,123,456.00 you also need to set the following properties

要获得像 RM 11,123,456.00 这样的格式,您还需要设置以下属性

CurrentCulture modified = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
Thread.CurrentThread.CurrentCulture = modified;
var numberFormat = modified.NumberFormat;
numberFormat.CurrencySymbol = "RM";
numberFormat.CurrencyDecimalDigits = 2;
numberFormat.CurrencyDecimalSeparator = ".";
numberFormat.CurrencyGroupSeparator = ",";

If you do that at application startup then that should make ms-MY format like en-US but with the RM currency symbol every time you call the ToString("C")method.

如果您在应用程序启动时执行此操作,则应使 ms-MY 格式类似于 en-US,但每次调用该ToString("C")方法时都带有 RM 货币符号。

回答by masoud ramezani

use this format string :

使用此格式字符串:

#,##0.00 $;#,##0.00'-  $';0 $
#,##0.00 $;#,##0.00'-  $';0 $
decimal paid = Convert.ToDecimal(dr["TotalPaids"]);
lblPaids.Text = paid.ToString("#,##0.00 $;#,##0.00'-  $';0 $");

回答by Jonathan

If I understand your question correctly what you want is to replace the $ with RM. If so, you need to pass the custom format...

如果我正确理解您的问题,您想要的是将 $ 替换为 RM。如果是这样,您需要传递自定义格式...

lblPaids.Text = paid.ToString("C", LocalFormat);

回答by Atish

lblPaids.Text = paid.ToString("C",usCulture.Name);

Or

或者

lblPaids.Text = paid.ToString("C",LocalFormat.Name);

must Work

必须工作

回答by Aaron Sherman

You can use the Double.ToString Method (String,?IFormatProvider) https://msdn.microsoft.com/en-us/library/d8ztz0sa(v=vs.110).aspx

您可以使用 Double.ToString 方法 (String,?IFormatProvider) https://msdn.microsoft.com/en-us/library/d8ztz0sa(v=vs.110).aspx

double amount = 1234.95;

amount.ToString("C") // whatever the executing computer thinks is the right fomat

amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ie"))    //  1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("es-es"))    //  1.234,95  
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-GB"))    //  £1,234.95 

amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-au"))    //  ,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us"))    //  ,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ca"))    //  ,234.95