在JSON中传递美元金额的最佳做法是什么? [英] What are the best practices passing dollar amounts in JSON?
问题描述
在JSON中传递美元金额的最佳做法是什么?
将金额作为字符串或浮点数传递?或其他?
我们有点担心客户端(iOS / Android / Web)上显示的错误,或者一个客户端显示的值与另一个客户端显示的值可能不同。
有效的Java,第二版Joshua Bloch说
float和double类型特别不适合进行货币计算,因为不可能将0.1(或十的任何其他负数)表示为float或double。
例如,假设您有$ 1.03,而您花费了42c。您还剩多少钱?
System.out.println(1.03 - .42);
打印出0.6100000000000001.
解决这个问题的正确方法是使用BigDecimal,int或long。
从不在浮动变量中持有货币值。浮点数不适用于货币使用定点或十进制值。
更好的是提供货币代码和相同的值在几个不同的格式。考虑这个典型的反应,金额为0.00234美元b
amount :
currency:USD,
decimal:0.00234,
integer:234000,
integer_scale:8,
漂亮:\\\฿ 0.00234 BTC,
string:0.00234
}
您可以使用任何提供的数字格式。
十进制:这是一个十进制数字
字符串:与十进制数字相同但引用,所以你的JSON库认为这是一个字符串。
pretty:准备向用户显示的字符串。包括适当的货币符号和货币代码。
currency:货币的三字母代码。
以下是两个示例API:
1 - JSON中的常见货币代码
{
USD:{
符号:$,
名称:美元,
symbol_native:$,
decimal_digits:2,
舍入:0,
code:USD,
name_plural:美元
},
CAD:{
符号:CA $,
name:加拿大元,
symbol_native:$,
decimal_digits:2,
四舍五入 :0,
code:CAD,
name_plural:加拿大元
},
EUR:{
符号: $,
name:Euro,
symbol_native:€,
decimal_digits:2,
四舍五入:0,
code:EUR,
name_plural:欧元
}
}
https://gist.github.com/Fluidbyte/2973986
$ b
2 - Fixer API
http://api.fixer.io/latest {
base:EUR,
AUD:1.4732,
BGN:1.9558,
BRL:3.7015,
CAD:1.4712,
CHF:1.1357,
CNY:7.9087,
CZK:26.048,
DKK:7.4364,
GBP:0.89568,
HKD:9.1613,
HRK:7.412,
HUF:304.93,
IDR:15639,
ILS:4.1765,
INR:75.256,
JPY:130.37,
KRW:1317.6,
MXN:20.809,
MYR:5.0229,
NOK:9.3195,
NZD:1.5694,
PHP:59.207,
PLN:4.2493,
RON:4.558,
RUB:69.832,
SEK:9.5355,
SGD:1.5947,
THB:39.146,
TRY:4.1462,
USD:1.1729,
ZAR:15.281
}
}
What are the best practices passing dollar amounts in JSON?
Passing the amounts as strings, or floats? or other?
We are a bit worried about round off errors displayed on client (iOS / Android / Web), or possibly different values displayed on one client compared to another.
Effective Java, 2nd Edition Joshua Bloch said
The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.
For example, suppose you have $1.03 and you spend 42c. How much money do you have left?
System.out.println(1.03 - .42); prints out 0.6100000000000001.
The right way to solve this problem is to use BigDecimal, int or long .
Never hold monetary values in a float variable. Floating point is not suitable for currency use either fixed-point or decimal values.
Better if provide the currency code and the same value in a few different formats. Consider this typical response for a amount of 0.00234
"amount": {
"currency": "USD",
"decimal": 0.00234,
"integer": 234000,
"integer_scale": 8,
"pretty": "\u0e3f 0.00234 BTC",
"string": "0.00234"
}
You have the option of using any of the provided number formats.
decimal: This is a decimal number string: Same as decimal but quoted so your JSON library thinks it is a string. pretty: A string ready to be shown to users. Includes appropriate currency sign and currency code. currency: The 3-letter code for the currency.
Following are two examples API :
1 - Common Currency Codes in JSON
{
"USD": {
"symbol": "$",
"name": "US Dollar",
"symbol_native": "$",
"decimal_digits": 2,
"rounding": 0,
"code": "USD",
"name_plural": "US dollars"
},
"CAD": {
"symbol": "CA$",
"name": "Canadian Dollar",
"symbol_native": "$",
"decimal_digits": 2,
"rounding": 0,
"code": "CAD",
"name_plural": "Canadian dollars"
},
"EUR": {
"symbol": "€",
"name": "Euro",
"symbol_native": "€",
"decimal_digits": 2,
"rounding": 0,
"code": "EUR",
"name_plural": "euros"
}
}
https://gist.github.com/Fluidbyte/2973986
2 - Fixer API
{
"base": "EUR",
"date": "2017-07-28",
"rates": {
"AUD": 1.4732,
"BGN": 1.9558,
"BRL": 3.7015,
"CAD": 1.4712,
"CHF": 1.1357,
"CNY": 7.9087,
"CZK": 26.048,
"DKK": 7.4364,
"GBP": 0.89568,
"HKD": 9.1613,
"HRK": 7.412,
"HUF": 304.93,
"IDR": 15639,
"ILS": 4.1765,
"INR": 75.256,
"JPY": 130.37,
"KRW": 1317.6,
"MXN": 20.809,
"MYR": 5.0229,
"NOK": 9.3195,
"NZD": 1.5694,
"PHP": 59.207,
"PLN": 4.2493,
"RON": 4.558,
"RUB": 69.832,
"SEK": 9.5355,
"SGD": 1.5947,
"THB": 39.146,
"TRY": 4.1462,
"USD": 1.1729,
"ZAR": 15.281
}
}
What is the standard for formatting currency values in JSON?
这篇关于在JSON中传递美元金额的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!