Wednesday, July 8, 2009

SharePoint regional settings double value bug

By Hristo Yankov

Here is an interesting one...

I had to set the regional settings of my SharePoint site to Hungarian, so the DateTime fields get localized properly. This has been discussed previously here.

However, I started experiencing problems with the double values. Explanation by example:
(myField is of type SPFieldNumber)

double myDouble = 123.45;
myField.DefaultValue = myDouble.ToString();

Basically, I am trying to assign the value of a type double variable as the default value of my SharePoint field (which I am about to render).

At this point, when I check (using Quick Watch in Visual Studio) the value of myField.DefaultValue it shows 123,45 (which is ok, for the localization I am using). However, myField.DefaultValueTyped shows 12345. Looks like SharePoint is ignoring the fact that the site is localized and does not consider ',' to be in the role of a '.'

The solution I came up with is:
double myDouble = 123.45;
myField.DefaultValue = myDouble.ToString(CultureInfo.InvariantCulture);

Perhaps it is not the best, but at least the output now is:
myField.DefaultValue = "123,45"
myField.DefaultValueTyped = 123.45

Which is what is expected! Let me know if you know of a better way!
Bookmark and Share

No comments: