9

I have a number like 202667.4. I want to convert this to number based on culture.

For Ex:

In "de"(German) the number should be in 202.667,40.

Any help will be greatly appreciated.

Thanks.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Manikandan Ram
  • 211
  • 1
  • 4
  • 12

4 Answers4

11

If you want to represent existing number (say, double) in culture specific format, try formatting:

https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

double source = 202667.4;

// "n"  - ... group separators, and a decimal separator with optional negative sign  
// "de" - German culture
string result = source.ToString("n", CultureInfo.GetCultureInfo("de"));

Console.WriteLine(result);

Outcome

202.667,40

If you are given a string and you want a number, put Parse (TryParse):

string data = "202.667,40";

double result = double.Parse(data, CultureInfo.GetCultureInfo("de"));

Console.WriteLine(data.ToString(CultureInfo.InvariantCulture));

If you don't want to specify the culture each time you work with formatting, you can set the culture as a current one:

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de");

...

double source = 202667.4;

Console.WriteLine($"{source:n}");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Tanks @Dmitry, It's worked. But, I want output as a number not as string. Any way to do this? – Manikandan Ram Nov 16 '17 at 07:38
  • @Manikandan Ram: number is always the same, but its representation (as a string) can *vary* (i.e. we want `202667.4` be represented in German culture with group and decimal separators), that's why we use formatting. – Dmitry Bychenko Nov 16 '17 at 07:41
1

You can use Culture info while parsing number into German format

Try with this approach:

string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:000,000.00}", <your number>)

for example:

 string result = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:000,000.00}", 202667.4)
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • I have tried. But, it’s not worked. System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("de"); double num = Double.Parse("202667.4", culture); 2026674.0 What I’m missing? – Manikandan Ram Nov 16 '17 at 07:31
  • 1
    I don't understand your comment @ManikandanRam, a `double` has no format. Formatting comes into play when interpreting a string to parse it, but it looks like it parsed your number string correctly, and it comes into play when formatting a string, which you don't do in the comment. – Lasse V. Karlsen Nov 16 '17 at 07:45
0

You can use this code to convert variables to different cultures:

int MyInt = 100;
string MyString = MyInt.ToString("C",CultureInfo.GetCultureInfo("de-DE"));
MessageBox.Show(MyString);

If you want to build all programs with a German format you can this code at your main class:

using System.Globalization;`

Application.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");`
Dale K
  • 25,246
  • 15
  • 42
  • 71
skazied
  • 46
  • 3
0
 System.Globalization.CultureInfo EnglishCulture = new 
System.Globalization.CultureInfo("en-EN");
System.Globalization.CultureInfo GermanCulture = new 
System.Globalization.CultureInfo("de-de");

neccesary transformation,

double val;
if(double.TryParse("65,89875", System.Globalization.NumberStyles.Float, 
GermanCulture,  out val))
{
string valInGermanFormat = val.ToString(GermanCulture);
string valInEnglishFormat = val.ToString(EnglishCulture);
 }

if(double.TryParse("65.89875", System.Globalization.NumberStyles.Float, 
EnglishCulture,  out val))
{
string valInGermanFormat = val.ToString(GermanCulture);
string valInEnglishFormat = val.ToString(EnglishCulture);
 }
Muhammad Ali
  • 129
  • 7