transformScale
Transform a Dinero object to a new scale.
When transforming to a higher scale, the internal amount value increases by orders of magnitude. If you're using the default Dinero.js implementation (with the number calculator), be careful not to exceed the minimum and maximum safe integers.
When transforming to a smaller scale, the amount loses precision. By default, the function rounds down the amount. You can specify how to round by passing a custom divide function.
For convenience, Dinero.js provides the following divide functions: up, down, halfUp, halfDown, halfOdd, halfEven (bankers rounding), halfTowardsZero, and halfAwayFromZero.
Parameters
| Name | Type | Description | Required |
|---|---|---|---|
dineroObject | Dinero<TAmount> | The Dinero object to transform. | Yes |
newScale | TAmount | The new scale. | Yes |
divide | DivideOperation | A custom divide function. | No |
Code examples
Transform an object to a new scale
js
import { dinero, transformScale } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 500, currency: USD, scale: 2 });
transformScale(d, 4); // a Dinero object with amount 50000 and scale 4Pass a custom divide function
js
import { dinero, transformScale, up } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 10455, currency: USD, scale: 3 });
transformScale(d, 2, up); // a Dinero object with amount 1046 and scale 2