Fixed.fromNum constructor
Creates a Fixed scale value from a double or integer value and stores the value with the given scale.
scale defaults to 16 if not passed.
This method will throw AmountTooLargeException if the scale is > 20 or the absolute value is greater than 10^21 This method will clip the no. of decimal places to 20.
final value = Fixed.fromNum(1.2345, scale: 2);
print(value) -> 1.23
For a decimal amount
we throw a AmountTooLargeException if an amount
is larger 10^21 is passed in.
An AmountTooLargeException will be thrown if the
scale > 20.
If you need larger numbers then use one of the alternate
constructors.
Implementation
Fixed.fromNum(num amount, {this.scale = 16}) {
_checkScale(scale);
final decoder = FixedDecoder(
pattern: '#.#',
groupSeparator: ',',
decimalSeparator: '.',
);
/// toStringAsFixed is limited to a max of 20 decimal places
try {
final fixed = amount.toStringAsFixed(scale);
if (fixed.contains('e')) {
throw AmountTooLargeException('The amount must be less than 10^20');
}
final decimalAndScale = decoder.decode(fixed, scale);
minorUnits = decimalAndScale.value;
// ignore: avoid_catching_errors
} on RangeError catch (_) {
throw AmountTooLargeException('The maximum scale for num is 20.');
}
}