0

I want to show culture on integers, but "n" number format gives me double format output.

Here is a small example

int i = 10000;
Console.WriteLine(i.ToString("n", CultureInfo.CreateSpecificCulture("en-US")));

This gives me out put as 10,000.00

I want output as 10,000

Workaround like modifying string is not what I need.

Is there any simple way to do this?

Kishor
  • 4,006
  • 7
  • 30
  • 47

4 Answers4

8

use n0 instead of n in the format.

int i = 10000;
Console.WriteLine(i.ToString("n0", CultureInfo.CreateSpecificCulture("en-US")));

Output would be:

10,000

Or you can use:

Console.WriteLine(i.ToString("N0"));

You may see: Standard Numeric Format Strings

The Numeric ("N") Format Specifier.

The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9),

By the way "N" or "n" are same

Habib
  • 219,104
  • 29
  • 407
  • 436
3
Console.WriteLine("{0:n0}",i, CultureInfo.CreateSpecificCulture("en-US"));

or closer to your original

Console.WriteLine(i.ToString("n0"), CultureInfo.CreateSpecificCulture("en-US"));

Live example: http://rextester.com/MDJI27648

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

What kind of number do you want to see? It is a monetary amount or what?:

c (currency) string.Format("{0:c}",i) $1,000,000.00

d (decimal) string.Format("{0:d}",i) 1000000

e (scientific) string.Format("{0:e}",i) 1.000000e+006

f (fixed point) string.Format("{0:f}",i) 1000000.00

g (general) string.Format("{0:g}",i) 1000000

n (with separator)string.Format("{0:n}",i) 1,000,000.00

x Hexadezimal string.Format("{0:x4}",i) f4240

Rafa
  • 2,328
  • 3
  • 27
  • 44
0

The number of post-decimal point digits printed by "n" is given by the NumberFormatInfo.NumberDecimalDigits property.

You can change this, and then use the CultureInfo in all your ToString calls to make sure your strings are consistent:

var customCI = CultureInfo.CreateSpecificCulture("en-US");
customCI.NumberFormat.NumberDecimalDigits = 0;

Console.WriteLine(i.ToString("n", customCI));
Rawling
  • 49,248
  • 7
  • 89
  • 127