在JSON中格式化货币值的标准是什么? [英] What is the standard for formatting currency values in JSON?

查看:147
本文介绍了在JSON中格式化货币值的标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到数据类型和本地化的各种怪异,Web服务与应用程序之间来回传递金钱价值的最佳方法是什么?某处有标准吗?

Bearing in mind various quirks of the data types, and localization, what is the best way for a web service to communicate monetary values to and from applications? Is there a standard somewhere?

我的第一个想法是简单地使用数字类型.例如

My first thought was to simply use the number type. For example

"amount": 1234.56

我已经看到许多关于使用浮点数据类型进行货币计算时缺乏精度和舍入误差的问题的争论,但是,我们只是传递值,而不是进行计算,所以没关系.

I have seen many arguments about issues with a lack of precision and rounding errors when using floating point data types for monetary calculations--however, we are just transmitting the value, not calculating, so that shouldn't matter.

EventBrite的JSON货币规范指定如下内容:

{
"currency": "USD", 
"value": 432, 
"display": "$4.32"
}

避免使用浮点值的Bravo,但现在我们遇到了另一个问题:最大可容纳的数字是多少?

Bravo for avoiding floating point values, but now we run into another issue: what's the largest number we can hold?

一个评论(我不知道这是不是真的,但似乎很合理)声称,由于JSON中的数字实现方式各不相同,因此您可以期望的最好结果是32位带符号整数.一个32位有符号整数可以容纳的最大值是2147483647.如果我们以次要单位表示值,则为21,474,836.47美元. 2100万美元似乎是一笔不小的数目,但并非无法想象某些应用程序可能需要以更高的价格运行.当货币中以1,000的次要单位为主要单位,或者货币的价值小于美元时,问题变得更加严重.例如,突尼斯第纳尔被分为1,000 milim. 2147483647 milim或2147483.647 TND为$ 1,124,492.04.在某些情况下,更可能价值超过100万美元.另一个例子:越南盾的子单位因通货膨胀而变得无用,所以我们只使用主要单位. 2147483647 VND为$ 98,526.55.我敢肯定,许多用例(银行结余,房地产价值等)都远高于此. (不过,EventBrite不必担心门票价格这么高!)

One comment (I don’t know if it’s true, but seems reasonable) claims that, since number implementations vary in JSON, the best you can expect is a 32-bit signed integer. The largest value a 32-bit signed integer can hold is 2147483647. If we represent values in the minor unit, that’s $21,474,836.47. $21 million seems like a huge number, but it’s not inconceivable that some application may need to work with a value larger than that. The problem gets worse with currencies where 1,000 of the minor unit make a major unit, or where the currency is worth less than the US dollar. For example, a Tunisian Dinar is divided into 1,000 milim. 2147483647 milim, or 2147483.647 TND is $1,124,492.04. It's even more likely values over $1 million may be worked with in some cases. Another example: the subunits of the Vietnamese dong have been rendered useless by inflation, so let’s just use major units. 2147483647 VND is $98,526.55. I’m sure many use cases (bank balances, real estate values, etc.) are substantially higher than that. (EventBrite probably doesn’t have to worry about ticket prices being that high, though!)

如果我们通过以字符串形式传递值来避免该问题,那么该字符串应如何格式化?不同的国家/地区具有截然不同的格式-不同的货币符号,符号出现在金额之前还是之后,符号和金额之间是否有空格(如果使用逗号或句点分隔小数)(如果使用逗号)用作千位分隔符,括号或减号,以表示负值,可能还有更多我不知道的值.

If we avoid that problem by communicating the value as a string, how should the string be formatted? Different countries/locales have drastically different formats—different currency symbols, whether the symbol occurs before or after the amount, whether or not there is a space between the symbol and amount, if a comma or period is used to separate the decimal, if commas are used as a thousands separator, parentheses or a minus sign to indicate negative values, and possibly more that I’m not aware of.

应用程序是否应该知道其使用的语言环境/货币,并传达类似的值

Should the app know what locale/currency it's working with, communicate values like

"amount": "1234.56"

来回,并信任该应用正确格式化金额吗? (另外:应该避免使用十进制值,而应以最小货币单位指定该值?还是应该在不同的属性中列出主要和次要单位?)

back and forth, and trust the app to correctly format the amount? (Also: should the decimal value be avoided, and the value specified in terms of the smallest monetary unit? Or should the major and minor unit be listed in different properties?)

还是服务器应提供原始值和格式化值?

Or should the server provide the raw value and the formatted value?

"amount": "1234.56"
"displayAmount": "$1,234.56"

还是服务器应该提供原始值和货币代码,然后让应用格式化? 数量":"1234.56" "currencyCode":"USD" 我认为无论使用哪种方法,都应双向使用,并与服务器进行双向传输.

Or should the server provide the raw value and the currency code, and let the app format it? "amount": "1234.56" "currencyCode": "USD" I assume whichever method is used should be used in both directions, transmitting to and from the server.

我一直找不到标准-您有答案吗,还是可以指向定义此标准的资源?这似乎是一个常见问题.

I have been unable to find the standard--do you have an answer, or can point me to a resource that defines this? It seems like a common issue.

推荐答案

我不知道这是否是最好的解决方案,但是我现在正在尝试的是将值作为无格式的字符串传递,除了小数点之外,例如所以:

I don't know if it's the best solution, but what I'm trying now is to just pass values as strings unformatted except for a decimal point, like so:

"amount": "1234.56"

应用程序可以轻松地对此进行解析(并将其转换为double,BigDecimal,int或应用程序开发人员最适合浮点算术的任何方法).该应用程序将负责根据地区和货币来格式化要显示的值.

The app could easily parse that (and convert it to double, BigDecimal, int, or whatever method the app developer feels best for floating-point arithmetic). The app would be responsible for formatting the value for display according to locale and currency.

此格式可以容纳其他货币值,无论是高膨胀的大数字,小数点后三位的数字,完全没有小数位的数字,等等.

This format could accommodate other currency values, whether highly inflated large numbers, numbers with three digits after the decimal point, numbers with no fractional values at all, etc.

当然,这将假定该应用程序已经知道使用的语言环境和货币(通过另一个调用,一个应用程序设置或本地设备值).如果需要在每次通话中指定这些选项,则另一个选择是:

Of course, this would assume the app already knows the locale and currency used (from another call, an app setting, or local device values). If those need to be specified per call, another option would be:

"amount": "1234.56",
"currency": "USD",
"locale": "en_US"

我很想将它们滚动到一个JSON对象中,但是JSON feed可能出于不同目的而具有多个金额,然后只需要指定一次货币设置即可.当然,如果每个列出的数量可能有所不同,那么最好将它们封装在一起,如下所示:

I'm tempted to roll these into one JSON object, but a JSON feed may have multiple amounts for different purposes, and then would only need to specify currency settings once. Of course, if it could vary for each amount listed, then it would be best to encapsulate them together, like so:

{
"amount": "1234.56",
"currency": "USD",
"locale": "en_US"
}

另一种值得商bat的方法是服务器提供原始数量和格式化数量. (如果是这样,我建议将其封装为一个对象,而不是在Feed中都具有定义相同概念的多个属性):

Another debatable approach is for the server to provide the raw amount and the formatted amount. (If so, I would suggest encapsulating it as an object, instead of having multiple properties in a feed that all define the same concept):

{
"displayAmount":"$1,234.56",
"calculationAmount":"1234.56"
}

在这里,更多的工作被卸载到服务器上.它还可以确保不同平台和应用程序在数字显示方式上的一致性,同时仍然为条件测试等提供易于解析的值.

Here, more of the work is offloaded to the server. It also ensures consistency across different platforms and apps in how the numbers are displayed, while still providing an easily parseable value for conditional testing and the like.

但是,这确实存在问题-如果应用程序需要执行计算然后向用户显示结果,该怎么办?仍然需要格式化显示的数字.最好在此答案的最前面使用第一个示例,并让应用程序控制格式.

However, it does leave a problem--what if the app needs to perform calculations and then show the results to the user? It will still need to format the number for display. Might as well go with the first example at the top of this answer and give the app control over the formatting.

至少这些是我的想法.我在这方面找不到任何可靠的最佳实践或研究,因此我欢迎未指出的更好的解决方案或潜在的陷阱.

Those are my thoughts, at least. I've been unable to find any solid best practices or research in this area, so I welcome better solutions or potential pitfalls I haven't pointed out.

这篇关于在JSON中格式化货币值的标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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