Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Survey use cases for BigDecimal (or similar types) #3
Would BigDecimal be useful for you? (Or, would Rational, Decimal128, or some other type be useful for you?) Why? Reports on this thread are encouraged to include:
Let's just collect use cases in this thread, and then we can consolidate/analyze it all in other issues.
Cases where you're not using a decimal type, and it's causing a bug (with code samples and context/motivation, and how a decimal type could fix it) are also welcome.
I guess I am not so surprised if rational took the wind out of decimal's sails in these cases; that matches what I have seen in other languages that include either fixed precision or arbitrary precision decimal: that there isn't a very popular ecosystem library for the other choice. Having a "standard" option is just so useful that it's not worth it to develop or seek out alternatives, even if they may be somewhat technically better for a particular case.
My understanding of the JS ecosystem is that the libraries for decimal are rather popular, and rational libraries are not so popular. Let's discuss the rational alternative in #6 and the JS ecosystem in #22 .
In company I currently work for (@g2a-com), we are dealing with a lot of financial data, which (obviously) cannot be safely represented using floats. We had to solve two problems with representing such data - how to store it in JSON and how to transform it within application.
From my experience currently there are three ways for solving these problems.
1. Integers using number type
Financial amounts can be represented with number type using the lowest subdivision of particular currency (euro cent for euro, kopek for ruble, yen for yen, etc). Such value has to be treated as an integer.
2. Integers using BigInt
Instead of using number to represent integers, you can use BigInts. By default BigInts aren't JSON serializable, but in this case they should be represented by strings.
3. Strings and decimals
Amounts can be stored in JSON using strings, which can be later wrapped with some decimals library (like decimal.js) for enabling making operations on them.
Native Decimal type would be obvious enhancement for 3rd solution, support for arithmetic operators would make working with decimals much easier. AFAIK Decimal128 have sufficient precision to deal with financial amounts (even when cryptocurrencies with come to play). Personally I would prefer to have BigDecimal over Decimal128, but in this case Decimal128 is enough.
I don't see any advantages of using Rational type for storing financial amounts.
I'm working at @omnea. We don't execute many monetary operations, but we want to have enough precision in the language to be safe. What we deal with more frequently are annoying issues with the DB and third party apps.
In our case:
MySQL decimal type
We would like to have a type to match the DECIMAL SQL type. One problem we recently hit is: sequelize/sequelize#7465
This forces the libraries to use strings in order to cover all cases. A native type would solve the issues and provide the DB libraries with a type able to match the DB types.
Third party systems
Our system communicates with many third party systems that, for some reason, send long decimals. It would help us if the language has the same capacities as the JSON format ones as it doesn't have a precision limit. Sometimes, parsing a JSON with a number field generated by another language can mean data loss.
Sometimes these decimals are there because other systems are not doing things "the correct way" but at the end we need to deal with it. Almost every other language has bigger floats types and that ends making harder to pass data between systems when one system has no way to parse data from the other system.
In summary, in our case BigDecimal would help us to communicate with other services with less effort. For us is more about having the ability to represent data without loosing precision rather than doing operations with it.