Scale

The Fixed package stores numbers with a fixed scale (number of decimal places).

If you attempt an operation on two Fixed values with different scales the result will be the larger of the two scales except when doing multiplication.

final t1 = Fixed.fromInt(12, scale: 1); // == 1.2, scale: 1
final t2 = Fixed.fromInt(2, scale: 2); // == 2.00, scale: 2
final t3 = t1 + t2; // == 2.20, scale: 2

final t3 = t1 * t2; // == 2.40, scale: 3

If you multiply two numbers the scale of the result will be the sum of the two scales.

final t1 = Fixed.fromInt(12, scale: 1); // == 1.2, scale: 1
final t2 = Fixed.fromInt(2, scale: 2); // == 2.00, scale: 2

final t3 = t1 * t2; // == 2.40, scale: 3

You can change the scale of a number by creating a new Fixed object using Fixed.copyWith.

Example 2

  final t7 = Fixed.fromNum(1.234, scale: 3); // == 1.234, scale: 3

  /// reduce the scale
  final t8 = Fixed.copyWith(t7, scale: 2); // == 1.23, scale: 2

  /// increase the scale
  final t9 = Fixed.copyWith(t8, scale: 5); // == 1.2300, scale: 5

Last updated