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

后端 未结 5 1935
长发绾君心
长发绾君心 2021-01-05 05:00

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

CultureInfo usCulture = new CultureInfo(\"en         


        
相关标签:
5条回答
  • 2021-01-05 05:28

    use this format string :

    #,##0.00 $;#,##0.00'-  $';0 $
    
    decimal paid = Convert.ToDecimal(dr["TotalPaids"]);
    lblPaids.Text = paid.ToString("#,##0.00 $;#,##0.00'-  $';0 $");
    
    0 讨论(0)
  • 2021-01-05 05:40

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

    lblPaids.Text = paid.ToString("C", LocalFormat);
    
    0 讨论(0)
  • 2021-01-05 05:40

    You can use the Double.ToString Method (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"))    //  $1,234.95
    amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us"))    //  $1,234.95
    amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ca"))    //  $1,234.95
    
    0 讨论(0)
  • 2021-01-05 05:53
    lblPaids.Text = paid.ToString("C",usCulture.Name);
    

    Or

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

    must Work

    0 讨论(0)
  • 2021-01-05 05:54

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

    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.

    0 讨论(0)
提交回复
热议问题