简体   繁体   中英

Format decimal number

I have decimal number always in US format, that is with decimal point. But some countries have comma instead of decimal point.

This will format string in current culture:

   string.Format("{0:F2} {1}", 100.0018, '€')

Result is: 100,00 and it has comma instead of point as wanted.

Is there some function to format on so many decimal points as it is input value, in my case 100,0018?

I have tried with this:

Decimal.TryParse("100.0018", System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out value);

but the result is 10000 instead of 100,0018

You, probably, want something like this:

 string.Format("{0:0.00###############} {1}", 100.0018, '€')

in order to have at least two digits after the decimal point:

100      -> 100.00 €      
100.0018 -> 100.0018 €  

这应该可以解决问题:

string.Format("{0:#,#.#####}", 100.0018)

You can use NumberFormat :

class Program
{
    static void Main(string[] args)
    {
        var brazil = new CultureInfo("pt-BR");
        var usa = new CultureInfo("en-US");

        Console.WriteLine(100.0018m.ToString(brazil.NumberFormat));
        Console.WriteLine(100.0018m.ToString(usa.NumberFormat));

        Console.ReadKey();
    }
}

My solution vould be: string.Format(new CultureInfo("en-US"), "{0:F2} {1}", 100.0018m, '€'); Result: 100.00 €

string.Format("{0:0.00###############} {1}", 100.0018, '€');

您还可以使用CultureInfo.CreateSpecificCulture获取货币

如果字符串,则可以使用replace,否则可以使用format

s = "100.0018".Replace(".", ",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM