-
money
The library supports arithmetic operations with monetary amounts, calculations with percentages, and allocation, making it simple to model use cases like installment payments (e.g., buy now, pay later), foreign exchange, investment yields, and tax collection. Cryptocurrencies are also fully supported out of the box. (by eriksencosta)
What do you think of this? https://github.com/eriksencosta/money/blob/enum-percentage-s...
It is a (quick) spike solution, but I've implemented the currency codes as enums and added support for 20.percent.
Not so sure about supporting 50.btc and 25.3.usd. I need to check if doing so would affect code completion.
-
SaaSHub
SaaSHub - Software Alternatives and Reviews. SaaSHub helps you find the best software and product alternatives
-
manifold
Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.
Generally, the idea is that there is no incompatibility between units. Money is a bit exotic given its abstract nature wrt units / exchange rates.
For instance, physical dimension store values as SI units, regardless of supplied unit, but retain the supplied unit type as a display hint.
Here, the Length dimension illustrates working with amounts of any unit.
Length orbit = 250 mi;
Length moon = 238000 km;
Length landing = moon + orbit;
display(landing); // prints 148,136.344 mi
display(landing.to(km)); // prints 238,402.336 km
// where
display(landing.to(mi) == landing.to(km)); // prints true
// perhaps a better example
Rate rate = 1.41 km/s;
Time duration = 76 hours;
Length distance = rate * duration;
// make your own unit constants
RateUnit kps = km/s;
rate = 1.41 kps;
You can play with the manifold-science[1] project, it implements the complete SI physical dimensions and many derived dimensions along with comprehensive operator overloading[2] support etc. to make working with and reading code using quantities more productive in Java.
1. https://github.com/manifold-systems/manifold/tree/master/man...
2. https://github.com/manifold-systems/manifold/tree/master/man...
-
How does it compare to the Java money API (https://jcp.org/en/jsr/detail?id=354) and the related Kotlin DSL in https://github.com/hiddewie/money-kotlin/?tab=readme-ov-file...?
-
I like the support for custom currencies, as that is an edge case that often pops up.
On the other hand, be careful about tying the symbol to the currency, as symbols are locale specific. For example, the symbol for USD is $ in eu-US but US$ in en-CA and en-AU (Canada and Australia), and then $US in French locales.
https://cldr.unicode.org/ is the magical dataset behind most good implementations that deal with currency display. Updated twice a year, available in JSON, providing currency symbols and formatting rules for all locales, as well as country => currency mappings and other useful information.
Disclaimer: I maintain a Go solution in this problem space: https://github.com/bojanz/currency
-
I'm surprised nobody has mentioned Joda Money yet:
https://www.joda.org/joda-money/
From the same person that brought is Joda Time (ie, what the java time API was based on). I've used Joda Money a lot and it's great.
Honestly I prefer APIs that look like APIs and I think this trend towards inventing DSLs is a bad one. Rails works because there's a critical mass of people who have adopted what is essentially a whole new language on top of Ruby. A money library doesn't warrant a new language, it's unnecessary cognitive load.
-
Nice. Looks like it satisfies all the requirements detailed here [0]. Also, good discussion of the major difficulty dealing with money here [1].
[0] https://cs-syd.eu/posts/2022-08-22-how-to-deal-with-money-in...
-
I'm working on a Clojure library for working with money [0] that handles specific rounding rules by passing a custom rounding function to the round function, allowing you to specify different rules as needed.
There's an example for the Swiss Franc in the README (the code is here [1]).
[0] https://github.com/sernamar/dinero
[1] https://github.com/sernamar/dinero/blob/main/src/dinero/roun...
-