2

I need to apply the format to the number of type decimal. Say a number 12345 must be shown in the format ## ##0,0. Then, the required output is 12 345,0.But somehow it does not apply correctly and I get the result as 1 2,345.

Below is the code that does the formatting work. Also check this fiddle where my issue is reproduced.

UPDATED

CultureInfo ci = new CultureInfo("fr-FR");
decimal integral = Convert.ToDecimal("12345");
Console.WriteLine(integral.ToString("## ##0,0"));

I know there are many format relevant solutions already available but unable to get what's wrong with this way.

JDoshi
  • 315
  • 4
  • 12
  • You dont appear to be using any of the ci detail you set... have you tried using it? – BugFinder Dec 30 '16 at 09:57
  • You have to set the current culture on the thread first because you are not using the new culture anywhere. How c# will know to use the culture you've created. – Dirty Developer Dec 30 '16 at 10:00
  • @DirtyDeveloper Yes, that's the mistake I did in creating the sample code. Can check the update and still the same issue. – JDoshi Dec 30 '16 at 10:06
  • Thank you for including a fiddle. Please continue helping people help you :). – Mr47 Dec 30 '16 at 10:31

3 Answers3

2

You are not using the created culture information to format the number. Also, your format specifier is incorrect. You must always use the POINT as decimal separator in the format string, which is then replaced by the culture-specific decimal separator (a comma for fr-FR).

The following should work correctly:

integral.ToString("## ##0.0", ci)
Mr47
  • 2,655
  • 1
  • 19
  • 25
  • With `.` it does work, but how can I achieve the same with `,`. I think it is possible with `,` as `decimal separator` but I am not aware how :( – JDoshi Dec 30 '16 at 10:15
  • The formats that needs to be applied are received from User Input. Generally it is supposed to work correctly with `,` as `decimal separator`. So this should work as it works well with `Excel`. – JDoshi Dec 30 '16 at 10:20
  • @JDoshi Unfortunately, the format specifiers are by design culture-independent. If you are certain about the culture of the end-user, you could perform a simple "replace" of the comma with a point. – Mr47 Dec 30 '16 at 10:22
  • 1
    If you are adamant the results must be formatted exactly like the user-provided string, you could also write your own formatter. – Mr47 Dec 30 '16 at 10:23
  • I am not aware of the culture of the end User. Thus in some cases it works well and in some it does not. – JDoshi Dec 30 '16 at 10:51
0

you can use this function :

public static string FormatNumber(this double number)
        {
            return number.ToString("#,##0.00");
        }

And then call your function like :

decimal integral = Convert.ToDecimal("12345");
Console.WriteLine(integral.FormatNumber());
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
0

I have provided answer here: https://stackoverflow.com/a/40443891/2963805

I've shown mutliple ways and hope that helps you.

Community
  • 1
  • 1
Dirty Developer
  • 551
  • 5
  • 22