Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
i have small query,how can i separated value with comas

for example if the value is 1241202,i want to separate like 1,241,202

any one can you please share any related code to me.
Posted
Updated 29-Sep-15 20:27pm

C#
string result = String.Format("{0:n0}", 1241202);//1,241,202


check .NET String.Format() to add commas in thousands place for a number[^] for more infomrmation
 
Share this answer
 
v3
Try:
C#
string result = string.Format("{0:n}", 1241202);

This will apply the appropriate separators (in the appropriate places) for the current system locale.
See here: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Maciej Los 30-Sep-15 2:38am    
5ed!
What you want to do is called getting a localized string representation of a number.
With your example, that would give:
C#
using System.Globalization;

// en-us culture represents numbers with comma as group separator, and point as decimal separator
CultureInfo usCulture = CultureInfo.CreateSpecificCulture("en-US");

int    intValue  = 1241202;
double realValue = 1241202.2;

// Will lead to "1,241,202"
string intValueRepresentation = intValue.ToString(usCulture);

// Will lead to "1,241,202.2"
string realValueRepresentation = realValue.ToString(usCulture);
 
Share this answer
 
Comments
Maciej Los 30-Sep-15 2:39am    
5ed!
phil.o 30-Sep-15 2:42am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900