C# 最佳实践 - 格式化多种货币
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/784794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Practice - Format Multiple Currencies
提问by
What is best practice for the scenario listed below?
下面列出的场景的最佳实践是什么?
We have an application which we would like to support multiple currencies. The software will respect the users locale and regional settings to dictate the correct number format, i.e. $10,000.00 or 10.000,00? etc.
我们有一个希望支持多种货币的应用程序。该软件将尊重用户的区域设置和区域设置来规定正确的数字格式,即 $10,000.00 还是 10.000,00?等等。
We would however like to be able to format different numbers based upon a currency ID (perhaps the three letter ISO4217 code). Our idea is to store a table in the database with each currency and then request from the user to select the currency symbol which will be used. The program will then format all numbers based upon the locale/region setting but will use the currency symbol as defined in our program.
然而,我们希望能够根据货币 ID(可能是三个字母的 ISO4217 代码)格式化不同的数字。我们的想是在数据库中存储一个包含每种货币的表,然后请求用户选择将要使用的货币符号。然后程序将根据区域设置/区域设置格式化所有数字,但将使用我们程序中定义的货币符号。
So, given the following values and currencies
10000 AUD
5989.34 USD
450 EUR
因此,给定以下值和货币
10000 AUD
5989.34 USD
450 EUR
the program would output
$10,000.00
$5,989.34
450.00
该程序将输出
$10,000.00
$5,989.34
450.00
or with a regional setting that formated numbers as #####,##$ the result would be;
10000,00$
5989,34$
450,00
或者使用将数字格式化为#####,##$ 的区域设置,结果将是;
10000,00$
5989,34$
450,00
Respecting locale/region number formatting but displaying different currencies, what is best practice for this?
尊重区域设置/区域数字格式但显示不同的货币,最佳实践是什么?
I hope this makes sense.
我希望这是有道理的。
Technology used is c# .NET 2.0.
使用的技术是 c# .NET 2.0。
回答by Iain Holder
You could store the money value as a decimal, then append the currency symbol on the UI.
您可以将货币值存储为小数,然后在 UI 上附加货币符号。
Fowler discusses the Money pattern in Patterns of Enterprise Application Architecturehere: http://www.martinfowler.com/eaaCatalog/money.html
Fowler 在此处讨论了企业应用程序架构模式中的货币模式:http: //www.martinfowler.com/eaaCatalog/money.html
回答by Elijah
It sounds like you have a handle on the best practices for formatting data. If I were you I would worry less about what is technically the standard but focus more on what it is your users are accustomed to.
听起来您已经掌握了格式化数据的最佳实践。如果我是你,我不会担心技术上的标准是什么,而是更多地关注你的用户习惯的标准。
If you are operating in markets where users are fairly savvy about different currencies it is a very different thing than having more monocultural users. Depending on the case, you will need to design your interface in a different way.
如果您在用户对不同货币相当了解的市场中运营,这与拥有更多单一文化用户截然不同。根据情况,您需要以不同的方式设计界面。
Moreover, if your requirement is to display anycurrency to anylocale, the ISO4217standard is your best bet. It is what is shown at currency exchange shops across the world, on currency exchanges, invoices, etc. Otherwise, displaying currency symbols could be rather confusing to some users and the symbol by itself does not indicate what currency the amount actually is.
此外,如果您的要求是在任何语言环境中显示任何货币,那么ISO4217标准是您最好的选择。它是在世界各地的货币兑换店、货币兑换处、发票等上显示的内容。否则,显示货币符号可能会让某些用户感到困惑,并且符号本身并不表示实际金额是哪种货币。
I would also reference the following SO questions. Not all of them directly relate to your problem, but they have very good meta discussions about the issues involved in supporting multiple currencies.
我还会参考以下 SO 问题。并非所有这些都与您的问题直接相关,但他们对支持多种货币所涉及的问题进行了很好的元讨论。
回答by MikeW
Rather than storing just the currency symbol, you could store the culture string for each currency code, so AUD --> en-AU
您可以存储每个货币代码的文化字符串,而不是仅存储货币符号,因此 AUD --> en-AU
CultureInfo ci = new CultureInfo("en-AU");
currencyValue.ToString( "c", ci );
I'm not sure how much flexibility there is in formatting available.
我不确定可用的格式有多大的灵活性。
Not sure if this helps:
不确定这是否有帮助:
回答by Ryan
I think this Q is an excellent and clear answer as to WHAT you should be doing
我认为这个问题是关于你应该做什么的一个很好而明确的答案
SO - Proper currency format when not displaying the native currency of a culture
And this Q has a good answer of HOW to do this in C#
这个 Q 有一个很好的答案,说明如何在 C# 中做到这一点
回答by Codex
I faced a similar situation. I found a way, dont know how useful it will be for you. But it solved my problems. I set a session string for each place like Session["cur"]="0.000" or "0.00" for each login authentication. And where ever currency comes in the system I used .ToString[Session["cur"].ToString()] to the model variables. It did the trick for me . Hope it helps you too .
我遇到了类似的情况。我找到了一个方,不知道它对你有多大用处。但它解决了我的问题。我为每个地方设置了一个会话字符串,例如每次登录身份验证的 Session["cur"]="0.000" 或 "0.00"。在系统中出现货币的地方,我将 .ToString[Session["cur"].ToString()] 用于模型变量。它对我有用。希望它也能帮助你。