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

Custom rounding modes #74

Closed
jonasesr opened this issue Dec 4, 2019 · 2 comments
Closed

Custom rounding modes #74

jonasesr opened this issue Dec 4, 2019 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jonasesr
Copy link

jonasesr commented Dec 4, 2019

Currently there is no possibility to set up a custom rounding function. Therefore you are limited to the ones that are implemented by default. For my case I need a rounding function that always rounds up (e.g. 0.002 => 0.01 with precision 2, commercial rounding)

It would be nice to pass a callback or a string that describes the rounding mode. If it is a string and the string matches the supported ones, the matching function gets executed. If it is a callback, the callback gets executed.

@jonasesr jonasesr added the enhancement New feature or request label Dec 4, 2019
@sarahdayan
Copy link
Collaborator

Hi @jonasesr! I'm totally open to a PR that implements this 🙂

This will be done like that in v2, but if you need it now, feel free.

@sarahdayan sarahdayan added the good first issue Good for newcomers label Mar 3, 2020
@sarahdayan
Copy link
Collaborator

Hey there! I just released Dinero.js v2 in alpha, and you no longer need to pass a rounding function when performing mutations.

You can still pass a rounding mode when formatting with toUnit or toFormat, and the library provides rounding functions for convenience. You can pass your own if you want to.

import { dinero, toUnit } from 'dinero.js';
import { USD } from '@dinero.js/currencies';

const d = dinero({ amount: 10555, currency: USD, scale: 3 });

toUnit(d, { digits: 1, round: Math.round }); // 10.6
toUnit(d, { digits: 1, round: Math.floor }); // 10.5
toUnit(d, { digits: 2, round: Math.trunc }); // 10.55

You can see it in action here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants