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

Format errors due to floating point issues in Moment.js #94

Closed
jsmreese opened this issue Jan 14, 2018 · 0 comments
Closed

Format errors due to floating point issues in Moment.js #94

jsmreese opened this issue Jan 14, 2018 · 0 comments
Milestone

Comments

@jsmreese
Copy link
Owner

jsmreese commented Jan 14, 2018

moment.duration(123.55, "hours").subtract(5, "days").asHours();
// 3.549999999999997

moment.duration(123.55, "hours").format("d[d] h[h]", 1)
// "5d 3.6h"

moment.duration(123.55, "hours").format("d[d] h[h]", 1, { forceFormatFallback: true })
// "5d 3.5h"

Resulting problem:

(3.549999999999997).toLocaleString("en", {
    minimumFractionDigits: 1,
    maximumFractionDigits: 1
});
// "3.6"

(3.549999999999997).toFixed(1);
// "3.5"

And the actual problem is because:

123.55 - 120;
// 3.549999999999997
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

1 participant