Rohit Chaudhari
Rohit Chaudhari

Reputation: 757

Add commas in decimal to string

I want to add a comma in the decimal to string conversion like

For

decimal number = 1000000000.000;

it should give

string str = 1,00,00,00,000.00

Upvotes: 2

Views: 1147

Answers (4)

King King
King King

Reputation: 63387

Of course, you can use some predefined culture, but I would like to introduce this way, with NumberGroupSizes to specify whatever format you want:

CultureInfo ci = new CultureInfo("en-US");
ci.NumberFormat.NumberGroupSizes = new int[] { 3, 2 };
Thread.CurrentThread.CurrentCulture = ci;
Console.WriteLine("{0:#,#.00}", 12345678.12);
//output 
1,23,45,678.12
ci.NumberFormat.NumberGroupSizes = new int[] {4, 2};
//output
12,34,5678.12
//....

Upvotes: 3

Stephan Bauer
Stephan Bauer

Reputation: 9249

You can achieve this by using the correct CultureInfo:

decimal input = 1000000000.0000m;
CultureInfo ci = new CultureInfo("hi-IN");
string output = string.Format(ci,"{0:#,#.00}",input);

By the way: following CultureInfos produce the correct output:

hi,bn,pa,gu,or,ta,te,kn,ml,as,mr,sa,kok,si,ne,
hi-IN,bn-IN,pa-IN,gu-IN,or-IN,ta-IN,te-IN,kn-IN,
ml-IN,as-IN,mr-IN,sa-IN,kok-IN,si-LK,ne-NP,bn-BD,en-IN

Upvotes: 6

Knn Taxsmart
Knn Taxsmart

Reputation: 36

You can do this,

String.Format("{0:#,###0}", 0);

Upvotes: -1

Snowcrack
Snowcrack

Reputation: 579

Here is a example:

http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx

specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays:    #,#.00#;(#,#.00#): (16,325.62)

Upvotes: 0

Related Questions