Simon
Simon

Reputation: 2035

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

Upvotes: 0

Views: 524

Answers (5)

Vivek Nuna
Vivek Nuna

Reputation: 1

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

You can get the Currency by using CultureInfo.CreateSpecificCulture also

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

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 €  

Upvotes: 1

Zoran Basic
Zoran Basic

Reputation: 156

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

Upvotes: 0

Rafael Marques
Rafael Marques

Reputation: 1874

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();
    }
}

Upvotes: 0

Mladen Oršolić
Mladen Oršolić

Reputation: 1422

This should do the trick :

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

Upvotes: 0

Related Questions