将整数转换为货币
我们有一个以基础货币存储数字(货币)的数据库,因此 £21.30 将在数据库中存储为 2130。我该如何将该数字转换为适用于所有文化的正确格式的货币?
分段阅读_第 2130 章21.30 英镑
分段阅读_第 2130 章$21.30
ETC
We have a database that stores numbers(money) in base currency, so £21.30 would be stored as 2130 in the database. How would I go about converting that number to a correctly formatted currency for all cultures?
2130 -> £21.30
2130 -> $21.30
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以快速扩展方法化:
这样你现在就可以变得更干燥:
Could be quickly extension methodified:
so that now you could be DRYier:
然而。这意味着 123.45 英镑变成了 123.45 美元或 123.45 欧元,这显然是不正确的。
还有一个问题是,比索/美元符号 $ 可以用来表示 NIO、AUD、CAD、TOP、USD、HKD 等等。它并不是唯一一个在多个地方使用的符号。如果您记录的是旧付款,甚至用于 IEP 和 ITL(不再使用)的英镑之类的事情也可能成为问题。
事实上,您将值存储为主要单位百分比的整数,这并不适用于所有货币。
货币价值仅对于隐含或显式货币才有意义。在这里,您要么采用一个没有的逻辑,要么用显式替换隐含的逻辑,要么依赖一大段压倒一切的逻辑来保持正确。
其中最后一个可以发挥作用,但也令人担忧。另外两个是错误的。
最后,文化最多只能告诉你人们最熟悉什么货币,但不能告诉你他们总是使用什么。我经常使用 kroner 和 øre,但在使用它们时我仍然使用 Hiberno-English。
我建议将 ISO 4217 代码与货币一起存储。最好将其也包含在最终发票上。符号有利于美观,代码有利于使事情精确。
忽略用户的文化,除非作为默认选择的最佳猜测。 (例如,如果他们是美国人,那么他们可能想使用美元,但可能不想)。世界上的货币并不多,所以这里列出了除一些专门的国际用例之外的所有货币:
哦,看。我上次更新时津巴布韦有自己的美元。哪个会发生得更快,您将其从列表中删除,或者更新框架来做到这一点?
However. This means that £123.45 becomes $123.45 or €123.45, which is clearly not correct.
Then there's the problem that Peso/Dollar sign $ can be used to represent NIO, AUD, CAD, TOP, USD, HKD, and a bunch more. It's not the only symbol that sees use in more than one places. If you're recording old payments even things like £ being used for IEP and ITL (no longer in use) can become an issue.
So can the fact that you're storing the values as whole numbers of percentage-of-main-unit, which doesn't apply to all currencies.
A monetary value is only meaningful with an implied or explicit currency. Here you're taking one that either has none, or you're replacing an implied with an explicit, or you're depending upon a very large piece of overriding logic to keep things correct.
The last of these can work, but is fraught. The other two are just wrong.
Finally, culture at best tells you what currency people are most familiar with, but not what they'll always use. I often use kroner and øre, but I use Hiberno-English when using them all the same.
I'd recommend storing ISO 4217 codes along with the currencies. It's a good idea to have it somewhere on a final invoice too. The symbol is good for prettiness, the code is good for having things precise.
Ignore the user's culture, except perhaps as a best-guess at a default choice. (e.g. if they're American then they probably want to use USD, but may not want to). There aren't that many currencies in the world, so here's a list of all bar a few specialised International-use cases:
Oh look. Zimbabwe had their own dollar the last time I updated. Which can happen quicker, you drop it from the list, or a framework is updated to do so?
以下是在 C# 中使用 string.format 执行此操作的方法:
这将为您提供当前区域性的格式。 有关 MSDN 的详细信息
Here's how to do it in c# with string.format:
That gets you the format for the current culture. More information on MSDN
请尝试以下操作:
Try the following: