Question

How can I change Numbers to the following:

1000 should become 1.000,00, when I have 700, it has to be 700,00.
When I try string.Format("{0:0,000.00}", Number), 700 becomes 0.700,00, so it's not good.

Was it helpful?

Solution

You need

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

You need to specify the lead placeholder as a # rather than a zero, which makes it optional.

However, rather than "brute force" to set the number format, it may be better to work out which culture's format you are aiming for and supply the correct CultureInfo to the string.format. String.Format lets you supply the culture for formatting as follows:

var culture = CultureInfo.GetCultureInfo("fr-FR");
var formattedNumber = string.Format(culture , "{0:n}", Number);

(I chose to use French purely as an illustration and because it seems to match the requirements in your examples).

What you shouldn't do, is use {0:n} without specifying the culture if you care about having a specific format - as this is entirely dependent on the culture settings of the user/system.

OTHER TIPS

Supply the proper CultureInfo in the string.Format

You want the decimal seperator to be , and the thousands separator to be .

Best way is to define this at application level for consistency.

Winforms:

In program.cs

System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator = ",";
System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator = ".";

ASP.NET

Define a base page, and do:

    protected override void InitializeCulture()
    {
    base.InitializeCulture(); 

     System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator = ",";    
     System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator = ".";                    
    }

When formatting a number using a format string, the . is a placeholder for whatever the decimal separator is for the culture being used for formatting. The , is similarly used as the thousands separator.

You need to use these in their correct place and use a culture that uses a , for a decimal separator and . for a thousands separator.

string.Format(CultureInfo.GetCultureInfo("de-DE"), "{0:n}", 1000)

Produces 1.000,00.

string.Format(CultureInfo.GetCultureInfo("en-GB"), "{0:n}", 1000)

Produces 1,000.00.

Did you try setting the NumberFormat as per your need

NumberFormatInfo num = new NumberFormatInfo();

num.NumberDecimalSeparator = ",";
num.NumberGroupSeparator = ".";
string samp =  70000.00.ToString("N2",num); // prints 70.000,00

Try this String.Format("{0:n}", 1000 )

Try String.Format("#,0.##"). It works for me.
Input: 800
Output: 800,00

Input: 1400
Output: 1.400,00

I use a German culture for the number format. Maybe you have to specify it, depending on your culture.

This will work:

NumberFormatInfo num = new CultureInfo("de-DE", false).NumberFormat;
double myDigit = 123456789; // you can also use int
string output = myDigit.ToString("N0" , num);
//output = 123.456.789

if you use ToString("F" , num) you can use 123456789.33 and your output will be 123.456.789,33

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top