Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: Amount must be an integer on test value 315.15 #35

Open
sofiaguyang opened this issue Oct 25, 2017 · 4 comments
Open

Comments

@sofiaguyang
Copy link

sofiaguyang commented Oct 25, 2017

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:

var resultAmount = amount * precisionMultiplier;

The result produced is 30014.999999999996 which is not the integer that's expected.

@sofiaguyang sofiaguyang changed the title Getting TypeError: Amount must be an integer on test value example 315.15 Getting TypeError: Amount must be an integer on test value 315.15 Oct 25, 2017
@MaxPRafferty
Copy link

MaxPRafferty commented Oct 31, 2017

Can also reproduce with new Money(34.98, currency). Will occur in any value in which n * 100 cannot be precisely held by JS float representation.
bad solution: next line, resultAmount = Math.ceil(resultAmount);
good solution: do math with an appropriately precise decimal library: https://github.com/MikeMcl/decimal.js/

@gitowiec
Copy link

gitowiec commented Apr 4, 2018

Just give your money in cents/pennies eg:

import MoneyLib = require('js-money');

MoneyLib.fromDecimal(30015, 'PHP');

@therealadamsimkins
Copy link

The Money.fromDecimal function allows you to pass a third parameter of "rounder" which should be one of Math.ceil, Math.floor, or Math.round. You can then create your Money object without error like so:
Money.fromDecimal(300.15, Money.PHP, Math.round);

@reestolonio
Copy link

@mothman103182 Passing a "rounder" function just burdens the application using it. You just remove the issue in the library and let application handles it. Why not use big decimal instead for precise numeric operations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants