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

[Bug]: toUnit does not round values with custom scales, when using dinero.js rounding functions #296

Closed
2 tasks done
jall opened this issue Jul 29, 2021 · 1 comment · Fixed by #295
Closed
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@jall
Copy link

jall commented Jul 29, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The digits argument to toUnit appears to be ignored in v2.0.0-alpha6 when a dinero value with a custom scale is passed: no rounding takes place

However, if I import the rounding function halfUp from @dinero.js/core directly, it works

Puzzled as to why this is, as the dinero.js package exports the rounding functions directly from @dinero.js/core anyway! I've never installed any other version of this package, and both package.lock and node_modules show all versions as 2.0.0-alpha.6

Expected behavior

The value is rounded to the specified number of digits and scaled appropriately

Steps to reproduce

Failing

import { dinero, halfUp, toUnit } from "dinero.js"
import {GBP} from "@dinero.js/currencies"

const d1 = dinero({amount: 123456789, scale: 5, currency: GBP})

const rounded = toUnit(d1, {
  digits: 2,
  round: halfUp,
})

console.log(rounded)
// Expected: 1234.57
// Actual  : 1234.56789

Working: "@dinero.js/core" rounding function import

import { dinero, toUnit } from "dinero.js"
import { halfUp } from "@dinero.js/core"
import {GBP} from "@dinero.js/currencies"

const d1 = dinero({amount: 123456789, scale: 5, currency: GBP})

const rounded = toUnit(d1, {
  digits: 2,
  round: halfUp,
})

console.log(rounded)
// Expected: 1234.57
// Actual  : 1234.57

Working: no custom scale

import { dinero, toUnit, halfUp } from "dinero.js"
import {GBP} from "@dinero.js/currencies"

const d1 = dinero({amount: 123456789, currency: GBP})

const rounded = toUnit(d1, {
  digits: 1,
  round: halfUp,
})

console.log(rounded)
// Expected: 12345678.9
// Actual  : 12345678.9

Version

2.0.0-alpha.6

Environment

macOS 11.5, Node 14.16.1, Typescript 4.3.4

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jall jall added the bug Something isn't working label Jul 29, 2021
@sarahdayan
Copy link
Collaborator

Hey @jall, that's a known bug that was fixed in #293. The rounding functions weren't properly re-exported from dinero.js, which is why it's failing. Right now, you're passing undefined.

This will go out in the 2.0.0-alpha.7 release which I'll do later today (see #295).

Note that the toUnit function currently has problems that I'm discussing in #294. Potential solutions might change its return type, or replace it with a different function. I'd love for you to participate the conversation!

Closing this once I release 2.0.0-alpha.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants