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

Documentation regarding creation of Dinero objects from floats in Dinero V2 #761

Open
quophyie opened this issue Oct 25, 2023 · 0 comments

Comments

@quophyie
Copy link

quophyie commented Oct 25, 2023

In FAQ docs in Dinero v2-alpha i.e (https://v2.dinerojs.com/docs/faq/how-can-i-create-dinero-objects-from-floats, follows from #58), the code is written as follows

function dineroFromFloat({ amount: float, currency, scale }) {
  const factor = currency.base ** currency.exponent || scale;
  const amount = Math.round(float * factor);

  return dinero({ amount, currency, scale });
}

Although the above is not incorrect, per se, it gives precedence to currency.exponent whereas I think it should give precedence to the user supplied scale . This is because when the caller calls the function with a scale, the caller would expect the supplied scale to be applied first and then if that fails, then try and apply the default scale of the currency

So I propose,

function dineroFromFloat({ amount: float, currency, scale }) {
 // NOT const factor = currency.base ** currency.exponent || scale;
// I propose it should be
  const factor = currency.base ** scale || currency.exponent ; 
// OR
// const factor =  currency.base ** (scale ?? currency.exponent) ;
  const amount = Math.round(float * factor);

  return dinero({ amount, currency, scale });
}
@quophyie quophyie changed the title Documentation regarding creation of Dinero objects from floats Documentation regarding creation of Dinero objects from floats in Dinero V2 Oct 25, 2023
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