I'm going to create an API which contains money amounts. I was wondering what the best practices are, or whether someone has some good or bad experiences with certain formats.
I've seen the following two possibilities:
I'm going back and forth between those two. So I went out to check what other APIs use and came up with the following list:
So, out of 18 sampled APIs, 4 are using minor units, 13 are using base units and 1 is using a hard-to-comprehend mixture. And within the 13 who use base units, 10 are transmitting them as quoted strings, 3 as unquoted decimals (actually 2 and a half if you look at Intuit).
I personally feel uncomfortable having to parse a string like "8.20", because if you parse this it becomes "8.19999999..." if you make the mistake to use floats. So I'm leaning towards sending integers only. But I don't think this is a great argument, and I see that generally APIs tend to go with base units as strings.
Do you have any good arguments for/against each format?
The goals of “Currency and Money” API: 1 To provide an API for handling and calculating monetary amounts 2 To define classes representing currencies and monetary amounts, as well as monetary rounding 3 To deal with currency exchange rates 4 To deal with formatting and parsing of currencies and monetary amounts More ...
Currency conversion is an important aspect of dealing with money. Unfortunately, these conversions have a great variety of different implementations and use cases. The API focuses on the common aspects of currency conversion based on the source, target currency, and exchange rate.
It's always associated with CurrencyUnit and defines a monetary representation of a currency. The amount can be implemented in different ways, focusing on the behavior of a monetary representation requirements, defined by each concrete use cases. For example. Money and FastMoney are implementations of the MonetaryAmount interface.
Amount of money should be represented as string. The idea of using string is that any client that consumes the json should parse it into decimal type such as BigDecimal to avoid floating point imprecision. However it would only be meaningful if any part of the system avoids floating point too.
Integers will eat the dot, that's one less byte :D Integers will have a max_int, do you have anyone rich enough that may overflow?
People that will parse a currency string as float will turn the int to float anyway.
If you send binary data, integer will be much smaller than a string and the way to go. If you send xml anyway, you might as well define it a string (the file is probably compressed before sending right?), try to make it "currency" type as opposed to listing it as a full string though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With