behzad razzaqi behzad razzaqi - 3 years ago 154
ASP.NET (C#) Question

Why bigint number not split with c# CultureInfo?

I'm beginner in C#, i have a currency field in SQL server, and that field's data type is


For example I have this number:


I want to show it in this format:


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:


What's happening? And how can I solve that problem?

Answer Source

If you look into NumberFormat of this CultureInfo you will see next:

var culture = CultureInfo.GetCultureInfo("es-US");
//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:

  1. Create specific culture, based on es-US and set NumberGroupSizes as [3].
  2. Use another existing CultureInfo with predefined NumberGroupSizes with [3] - for example, en-US.
  3. Use special formatting, as Ali suggested

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download