views:

232

answers:

3

I am using SPFieldCurrency column in one of my lists.

My custom code receives a string value as a parameter, which contains the field's value as returned by the GetFormattedValue() method.

Now my problem is that the value received by my method contain currency symbols in them, Eg, 10$, 10¥, 10€ etc.

Because of the presence of the currency symbols in my code, when I do a Double.TryParse() on these values, it fails.

How do I extract the numerical value from the display string value of an SPFieldCurrency object, without knowing the culture info of the currency?

A: 

"You can do this by creating a NumberFormatInfo instance and setting the properties on that to handle the particular aspects of your string. For example, you want to set the CurrencySymbol property to "$", the NumberDecimalDigits property to 2, etc."

source

The only issue you have i guess is that there are multiple types of currency in the field...

Colin
Yes, I don't know which currency is being used in the incoming string.
ashwnacharya
A: 

Well, if you simple parse the value directly like this:

double myvalue;
bool success = double.TryParse(properties.ListItem["MyFieldName"].ToString(), out myvalue);

You will have the value without the currency symbol.

But your question is somewhat ambiguous, is the problem to get the value out from the SharePoint item as a double, or is the problem how you can handle the formatted string in your code?

Magnus Johansson
Hi, my function recieves as parameter, the value of the sharepoint item, as a string, including the currency symbol.
ashwnacharya
+1  A: 

Got it. The gotcha is to use NumberStyles.Any. This removes all currency symbols.

I used Double.TryParse(valueString, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out value)

It worked for me. Thanks

ashwnacharya
So this has nothing to do with SharePoint at all, right?
Magnus Johansson