C# string.Format 1000,00 to 1.000,00

c#, string, string.format

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.

Problem

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.

Original source