我使用此代码将货币USD转换为Double [英] I use this code for convert Currency USD to Double

查看:90
本文介绍了我使用此代码将货币USD转换为Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码在VisualStudio 2008 Bussiness情报服务中将美元货币转换为Double,但它产生错误:

I use this code to convert currency in USD to a Double in VisualStudio 2008 Bussiness intelligence service, but it''s producing an error:

=System.Double.Parse(Fields!new_cost.Value,System.Globalization.NumberStyles.Currency)



或this


or this

=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!

推荐答案

对你的问题不是一个完整的答案,但如果你能避免它,就不要使用double,或者你会得到奇怪的浮点错误,试试运行这段代码:



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:

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 是假的!当然不是你在处理钱时所需要的,所以请改用十进制。





您的代码可能不会产生双倍以公认的格式。值得在代码上设置断点并运行 Fields!new_cost.Value.ToString()以查看传递给parse方法的内容。取决于值是什么,它甚至可能是类名...如果您向我们提供错误消息,那将有所帮助。


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.


因为您想要独立于文化,所以可以给予这是尝试通过解析它加倍:



Since you want to be culture independent maybe give this a try by parsing it to double:

double x = double.Parse(currencyValue, NumberStyles.Currency, cultureInfo);
//Or
double y = double.Parse(currencyValue, NumberStyles.Currency);


这篇关于我使用此代码将货币USD转换为Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆