Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I use this code to convert currency in USD to a Double in VisualStudio 2008 Bussiness intelligence service, but it's producing an error:
C#
=System.Double.Parse(Fields!new_cost.Value,System.Globalization.NumberStyles.Currency)

or this
C#
=System.Convert.ToString((System.Int32.Parse(Fields!new_cost.Value,System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))))

Can anyone help to me with this?

Thanks in advance!
Posted
Updated 7-Feb-13 2:21am
v4
Comments
CHill60 7-Feb-13 8:32am    
What is the error it produces?

Not a full answer to your question, but don't use double if you can avoid it, or you'll get odd floating point errors, try running this code:

C#
double d1 = 0.31;
double d2 = 0.27;
double sum = d1 + d2;
bool expectedValue = sum == 0.58;
Console.WriteLine("d1:{0} d2:{1} result {2} equals expected {3}", d1, d2, sum, expectedValue);

expectedValue is false! Certainly not what you'd want when dealing with money, so use decimal instead.


You code is probably not producing a double in a recognised format. It is worth putting a breakpoint on your code and running Fields!new_cost.Value.ToString() to see what is being passed into the parse method. Depending on what the value is, it might even be the class name... If you provide us with an error message, that would help.
 
Share this answer
 
Comments
Bernhard Hiller 8-Feb-13 4:20am    
That simple example is great.
When doing math with money, I normally use int (i.e. the amount is in cents, not dollars/euros), as all databases know some kind of int.
Keith Barrow 8-Feb-13 4:28am    
Int is also a good way to go, that is what we do where I work.
Since you want to be culture independent maybe give this a try by parsing it to double:

C#
double x = double.Parse(currencyValue, NumberStyles.Currency, cultureInfo);
//Or
double y = double.Parse(currencyValue, NumberStyles.Currency);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900