1

I am getting some percent values from a database and I need to format them to have the correct thousands seperator, number of decimal places and a percent sign on the end.

I tried this:

string text = "105,3"; //example, formatting like database input
string format = "#,##0.##";

e.Row.Cells[i].Text = double.Parse(text).ToString(format);

Weirdly this returns 1053,00%. How do I make it so it returns 105,30%? (The decimal comma is because the system locale is german, so it's how it is supposed to be)

edit: replacing the comma with a period results in 10530.00%. Nothing makes sense to me anymore.

edit2: the float.Parse() actually works just fine. the ToString() messes everything up. I played around with using different cultural settings and format strings (switching comma and period) but it only makes it worse again.

lenny
  • 734
  • 2
  • 15
  • 43
  • I cannot reproduce this (tried using `CultureInfo.GetCultureInfo("de-DE");`) - I get `105,3` – Matthew Watson Mar 17 '17 at 09:37
  • Same here, used your code and got the correct result. – Mighty Badaboom Mar 17 '17 at 09:38
  • @MatthewWatson didn't work for me, neither with `CultureInfo.CurrentCulture` nor with `CultureInfo.GetCultureInfo("de-DE")` – lenny Mar 17 '17 at 10:11
  • I suspect this must be to do with some formatting used for `e.Row.Cells[i]` and nothing to do with the value returned from `double.Parse(text).ToString(format);` – Matthew Watson Mar 17 '17 at 10:36
  • try to store result of parsing to string - you could check then if there is a problem with parse method (suppose not); And then try to assign to Cell[i] custom string, for example "105,30"- I think there would be a problem. Maybe with your UI culture setting – daniell89 Mar 17 '17 at 10:40
  • maybe check this: http://stackoverflow.com/a/9104557/5358389 – daniell89 Mar 17 '17 at 10:42

2 Answers2

3

Pass the current Culture to the Parse method: double.Parse( text, CultureInfo.CurrentCulture )

However, this only works on systems that use a locale that has the comma as a decimal separator. If you want this to work on other locales you should replace CurrentCulture with the specific CultureInfo instance that used when inputting data in the first place.

Sylence
  • 2,975
  • 2
  • 24
  • 31
  • there is no overload that takes cultureinfo as parameter, do you know of another function that does? – lenny Mar 17 '17 at 09:54
  • Yes there is: https://msdn.microsoft.com/en-us/library/t9ebt447(v=vs.110).aspx (CultureInfo implements the IFormatProvider interface) – Sylence Mar 17 '17 at 09:58
  • you're right, it takes it as a parameter. sadly it still outputs it the same way, even with german culture hardcoded – lenny Mar 17 '17 at 10:09
  • Did you make sure that the result of the Parse and ToString calls are correct? If so the problem is probably that the DataGrid is using a wrong culture to format the passed value. – Sylence Mar 17 '17 at 11:16
  • by now I figured out that the actual problem is the `ToString()`. It messes it whether I use a comma or a period as decimal seperator – lenny Mar 17 '17 at 11:51
0

The title is misleading. The actual problem was the ToString() function. In the format string I added the % sign, which, to be fair, I didn't add in the original post because I forgot about it. It automatically multiplies the number by 100. So my format string is now "#,##0.00\%".

lenny
  • 734
  • 2
  • 15
  • 43