davidkalosi / js-money Goto Github PK
View Code? Open in Web Editor NEWJavaScript Implementation of Fowlers Money object.
License: MIT License
JavaScript Implementation of Fowlers Money object.
License: MIT License
I have a use case where I'm treating points in my shopping cart as a custom currency. Would be great to give the user the ability to initialize/configure with custom currency options.
The code snippet below throws Getting TypeError: Amount must be an integer
import MoneyLib = require('js-money');
MoneyLib.fromDecimal(300.15, 'PHP');
This line appears to be causing the problem:
Line 119 in 10a4870
The result produced is 30014.999999999996
which is not the integer that's expected.
The documentation states that, for example, to add money, one can do the following:
fiveEur.add(250, Money.EUR); // 7.50 EUR
However, inspecting the code and testing shows that this is not the case; instead, the correct way seems to be:
fiveEur.add(new Money(250, Money.EUR))
Is that correct? If so, I guess the docs should be fixed to reflect that - I can send a PR if you don't have the time, just wanted to check first.
The currency definition for Zambian Kwacha uses currency code, that went out of use in 2012.
Previous code: ZMK
Current code: ZMW
I noticed there is a pull request, that already fixes this issue, among others. Could you please consider merging the PR?
Thank you for this great package!
Is there a reason you have decided to not always append decimals to the amount? It might seem confusing when you make money out of 1000 you get 1000, but when you make money out of 1000.00 you get 100000.
Should it not be consistent?
I'm unfamiliar with publishing npm packages - how can we get 0.5.0 into npmjs.com?
Is there a built-in mechanism for currency conversion arithmetics?
Is there a reason why BTC hasn't been added to currencies.js? If there is no problem, I'd do a PR with this:
"BTC": { "symbol": "BTC", "name": "Bitcoin", "symbol_native": "B͈̎", "decimal_digits": 8, "rounding": 0, "code": "XBT", "name_plural": "Bitcoins" },
I'm implementing this for the first time so perhaps this is something obvious but if I want to eventually output the money as a decimal is this not something which should live within the money object code?
Seems odd that you would safely be able to do calculations whilst it is represented as cents but then you risk causing an issue by having to do the toDecimal implementation yourself.
Thanks in advance for any advice
Hi, I would like to know if this repository is being maintained. I saw there haven't been commits for a long time.
If not, do you know any alternative?
I'm interested on the converter interface mentioned in #29.
Also, I think the source code is a little bit outdated, using prototypes makes it harder to read, maybe it could be rewritten in TypeScript or at least plain JavaScript with classes
The symbol for Rupee should be ₹
Multiplying these two numbers together seems to yield the wrong result:
Money.fromDecimal(2090.50, Money.USD).multiply(Money.fromDecimal(8.61, Money.USD)).toDecimal() // = 17999.2
Should be 17999.21.
Internally I think this is what happens:
209050 * 8.61 // = 1799920.499999999....
Math.round(1799920.4999999998) // = 1799920
While using your library, I ran across some unexpected behavior.
require('js-money').fromDecimal(78.13, 'USD');
{ amount: 7813, currency: 'USD' }require('js-money').fromDecimal(78.43, 'USD');
TypeError: Amount must be an integer
A little digging into the code revealed that this is just a variant of the floating point rounding errors that I had hoped to avoid in the first place.
78.13*100
781378.43*100
7843.000000000001
This then trips the check in the Money constructor which throws the above TypeError. Is there any feasible workaround for this?
In my automated testing I found out that passing the amount of 142.45 throws:
TypeError: Amount must be an integer
All the other amounts like 132.45 work fine. What is the issue?
Hi! Looks like this library lacks the following methods:
It will significantly improve readability of code that was written with the usage of this library.
I can see the need to internally store the amount in a different format than a floating point Number, but I think it would make total sense to hide that implementation detail from the rest outside world. Otherwise, you easily end up with situations where suddenly, $100 has grown into $10000 for no apparent reason, aside from the joy it might bring to your accounting department. :)
Look at these examples:
> (new Money(1, Money.USD)).compare( new Money(1, Money.USD) )
0 // true
> Money.fromDecimal(1.0, Money.USD).compare( new Money(1, Money.USD) )
1 // uh oh
I've been researching how to do this, but I'm a bit at a loss. Any idea on how to implement AMD support? I am currently using browserify to load this into Ember, but an AMD option would be nice.
Currently the Money.fromDecimal relies on Math.pow(10, currency.decimal_digits)
and will not work correctly with some numbers unless a rounder like Math.ceil
is passed in when being used.
Example:
var money = Money.fromDecimal('2067.22', 'USD');
Result:
206721.99999999997
Generates error for isInt check on line 63 & 64:
if (!isInt(amount))
throw new TypeError('Amount must be an integer');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.