Performing math

Fixed provides overloads for the full set of mathematical operations.

When performing most mathematical operations the scale of the result is the larger scale of the two operands.

Example:

final t1 = Fixed.fromNum(1.234: scale: 3);
final t2 = Fixed.fromNum(1.0, scale: 1);

final t3 = t1 + t2; /// == 2.234, scale: 3

For multiplication, the resulting scale is the sum of the operands scales e.g.

final t1 = Fixed.fromNum(0.01, scale:2) * Fixed.fromNum(0.02, scale:2); 
print(t1); //  = 0.0002, scale: 4

If you need to change the scale of a number use Fixed.copyWith passing the required scale.

 Fixed.copyWith(Fixed.fromInt(5, scale: 2), scale: 10);

Operators

The support operators are:

*  - multiplication
/  - division
%  - modulus
+  - addition
-  - subtraction
-  - unary minus (e.g. -1)
~/ - truncating division 

Example 6

final t1 = Fixed.parse('1.23'); // = 1.23
final t2 = Fixed.fromInt(100, scale: 2); // = 1.00

t1 + t2; // == 2.23
t2 - t1; // == -0.23
t1 * t2; // 1.2300
t1 / t2; // 1.23
-t1; // == -1.23

Last updated