0

Hi i have a us number with decimal, which i try to convert to a danish number format. But somehow, i does not convert correctly

  CultureInfo us = new CultureInfo("en-US");
  CultureInfo dk = new CultureInfo("da-DK");


  double data = double.Parse("98.5");
  Console.WriteLine(data.ToString("N", dk));

My understanding is that .5 is the decimal i my instance. What i am trying to acheive is to make in into 98,5 instead. But when i try to do the following in my test code, it returns 985,0.

I need it to happen dynamic. A user could have saved the value as a string "98.5", and when he visit the value, it will be displayed correct because it displays in his culture.. But what if a different user, with a different culture visits the value. Then i would not be able to use cultureInfo "us" and "dk"?

Mags
  • 57
  • 7

1 Answers1

0

Try this, It will work for you.

CultureInfo us = new CultureInfo("en-US");
CultureInfo dk = new CultureInfo("da-DK");


double data = 98.5;
Console.WriteLine(data.ToString("N1", dk));
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • It works, now i just have to figure out, how to convert the data dynamic. Like if the user entered the value as 98,5, and then switched language to us, then the value should be displayed as 98.5 – Mags Jun 22 '20 at 10:57
  • @Mags ` double data = double.Parse("98,5", dk); Console.WriteLine(data.ToString("N1", us)); ` – Vivek Nuna Jun 22 '20 at 11:07
  • What if i don't know what way the value was stored, only that it was stored as a string, and i dont know what culture the user has, that see the value. Then i couldn't write us or dk in ToString() – Mags Jun 22 '20 at 11:12
  • you should know the culture, otherwise, it will work as default. – Vivek Nuna Jun 22 '20 at 11:14
  • The problem is i don't. I only have the string, and then somehow has to figure out what format, the user had who saved the value, and then parse it to a new format, when a user with different culture sees it – Mags Jun 22 '20 at 11:17
  • @Mags you can use Regex to check the value based on the pattern you can decide, but I don't think that is a good solution. you can know the current culture, but can not know the format – Vivek Nuna Jun 22 '20 at 11:20