views:

61

answers:

3

I am trying to create a NumberFormat that will not use Groups at all.

I would like all numbers to be displayed with NO commas.

Example:

  • 1999 instead of 1,999
  • 2000000 instead of 2,000,000
  • etc...

Unfortunately, I am using a 3rd Party control that is a NumericEditor and it applies a CultureInfo setting on it to show commas. So I need to create a CultureInfo instance that doesn't use Grouping at all.

I have tried this:

int[] groupSize = {0};
CultureInfo culture = new CultureInfo("en-US");
culture.NumberFormat.NumberGroupSizes = groupSize;

Also...

 CultureInfo culture = new CultureInfo("en-US");
 culture.NumberFormat.NumberGroupSeparator = String.Empty; // Throws and exception with the 3rd party control

The closest I have gotten is...

 CultureInfo culture = new CultureInfo("en-US");
 culture.NumberFormat.NumberGroupSeparator = " ";

I don't like this solution at all because instead of a comma its white space and it definitely looks odd.

Any ideas?

A: 

Try setting the NumberGroupSizes to a 1-dimensional array containing only a zero

GôTô
That's *exactly* what gmcalab is doing in the first chunk of code shown in the question.
LukeH
oops... but I've read it works fine
GôTô
@GôTô: Yes, that works fine for me too in standard .NET code. I suspect that the OP's problem is with the third-party library.
LukeH
+1  A: 

What 3rd-Party control are you using and have you tried reflecting (using something like .NET Reflector) to see how they are using the CultureInfo class, because in regular old C# code, setting the NumberFormat.NumberGroupSize = new int[]{0}; works as well as NumberFormat.NumberGroupSeperator = String.Empty;. It seems the 3rd Party control might be using the CultureInfo properties in a non-standard way.

ben f.
A: 

Consider implementing custom formatter.

You can assign your custom format provider like this:

CultureInfo.CurrentCulture.NumberFormat = new MyCustomFormatter();

That might work if they (3rd party control developers) didn't implement some "magic".

Paweł Dyda