Reputation: 53
I'm beginner in C#, i have a currency field in SQL server, and that field's data type is bigint
For example I have this number:
12598546
I want to show it in this format:
12,598,546
My C# code for it:
query_statusOfRPTOST[i].price_year.ToString("N0", CultureInfo.CreateSpecificCulture("es-US"))
That code in my local PC works fine, but when application runs in server, number is shown in following format:
12598,546
What's happening? And how can I solve that problem?
Upvotes: 2
Views: 168
Reputation: 4261
If you look into NumberFormat
of this CultureInfo
you will see next:
var culture = CultureInfo.GetCultureInfo("es-US");
Console.WriteLine(string.Join(",",culture.NumberFormat.NumberGroupSizes));
//output: 3,0
What means, according to msdn that to the left of decimal separator first group will contain 3 digits, and other will not be groupped.
If you want to get required value presented in format with groupping, you should do the one of following:
es-US
and set NumberGroupSizes
as [3]
. CultureInfo
with predefined NumberGroupSizes
with [3]
- for example, en-US
.Code example for first:
long a = 12598546;
var culture = CultureInfo.CreateSpecificCulture("es-US");
culture.NumberFormat.NumberGroupSizes = new int[] { 3 };
Console.WriteLine(a.ToString("N0", culture));
// output 12,598,546
Code example for second:
long a = 12598546;
Console.WriteLine(a.ToString("N0", CultureInfo.GetCultureInfo("en-US")));
// output 12,598,546
Upvotes: 1
Reputation: 9927
You can use string.Format like this:
string.Format("{0:#,###0}", query_statusOfRPTOST[i].price_year)
Upvotes: 1