Creating from floats
Dinero objects must be instantiated with integers, in minor currency units. For example, to create an object for $19.99, you should write the following code:
js
import { dinero, add, subtract } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 1999, currency: USD });If you have amounts as floats (in this case, 19.99) and you want to abstract object creation, you can write your own helper.
js
function dineroFromFloat({ amount: float, currency, scale }) {
const factor = currency.base ** (currency.exponent || scale);
const amount = Math.round(float * factor);
return dinero({ amount, currency, scale });
}INFO
This code isn't tested and not guaranteed to cover edge cases. Use it as a starter and make sure it works for you by testing it in your application.