转换任何货币字符串翻番 [英] Convert any currency string to double
问题描述
我需要多种货币存储在SQL服务器。据我所知,SQL不支持所有不同类型的货币(除非我把它存储为一个字符串,但我不想这样做)。
I need to store multiple currencies in SQL server. I understand that SQL won't support all different types of currencies (unless I store it as a string, but I don't want to do that).
我的想法是将所有值从其货币格式转换为标准的双人间和商店来代替。然后,只需重新格式显示时,基于文化信息。不过,我曾尝试做这样的事情例如。
My idea was to convert all the values from their currency format to a standard double and store that instead. Then just re-format based on the culture info when displaying. However, I have tried doing something like e.g.
var cultureInfo = new System.Globalization.CultureInfo("en-US");
double plain = return Double.Parse("$20,000.00", cultureInfo);
这不会永远似乎工作,它总是抛出一个 FormatException
。即使在拆除货币符号和只是想仅仅基于数量做同样的事情做到这一点。这仅仅是我想支持pretty得多的任何类型的货币的一个例子。
This doesn't ever seem to work it always throws a FormatException
. Even removing the currency symbol and just trying to do this based on the number alone does the same thing. This is just an example I want to support pretty much any type of currency.
有剥离出来的货币和获取价值的双重的标准方式?
Is there a standard way of stripping out currency and getting the value as a double?
推荐答案
我觉得这应该工作:
double.Parse(currencyValue, NumberStyles.Currency);
在这里你可以看到更多有关的NumberStyles 。
编辑:万一有人看到这个答案不看其他的答案/评论,这个答案回答了一个问题,编写的,但储存货币作为一个双击
不是好主意,这将是更好地使用小数代替
In case anyone sees this answer without looking at the other answers/comments, this answer answered the question as written, but storing currency as a double
is not a good idea, and it would be better to use decimal instead.
这篇关于转换任何货币字符串翻番的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!