I am developing a function that, passing a number, formats it with the thousands separators and the decimal separators (if it has them) taking into account the cultural information of the user in question, and I have the following problem:
I have this piece of code:
public string FormatearNumero(object Num)
{
CultureInfo Culture = new CultureInfo("en-EN", false);
NumberStyles NStyle = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign;
string Numero = Num.ToString();
if (decimal.TryParse(Numero, NStyle, Culture, out decimal result))
{
if (result == 0)
return Numero;
Numero = result.ToString("###" + Culture.NumberFormat.NumberGroupSeparator + "###" + Culture.NumberFormat.NumberDecimalSeparator + "####", Culture);
}
return Numero;
}
To format the number I use the typical one .ToString("###.###,###")
but in this case applying in the thousands separators the one that corresponds to it by cultural reference and the same for the decimal separator.
result.ToString("###" + Culture.NumberFormat.NumberGroupSeparator + "###" + Culture.NumberFormat.NumberDecimalSeparator + "####", Dades.Culture)
Applying this portion of code with culture en-EN
I get the following results:
//Culture.NumberFormat.NumberGroupSeparator tiene el valor ','
//Culture.NumberFormat.NumberDecimalSeparator tiene el valor '.'
FormatearNumero("-98745618451.6513") --> "-98,745,618,451.6513"
Instead, when I change the cultural reference and put it, for example, es-ES
the following happens:
//Culture.NumberFormat.NumberGroupSeparator tiene el valor '.'
//Culture.NumberFormat.NumberDecimalSeparator tiene el valor ','
FormatearNumero("-98745618451,6513") --> "-98745618451,6513"
It does not apply the formats correctly for some cultures, but for others it does.
I have tried to search for information about it but I have not found anything, and on the pages of Microsoft
they do not indicate anything to take into account when applying these formats.
Could someone tell me where the problem could be?