# Arithmetic Operations

The Money class is immutable, so each operation returns a new Money instance.

Money uses fixed scale (no. of decimals) mathemtatics operations which can lead to some expected results.

import 'money2.dart';

/// 0 decimal place currency.

final Currency kr = Currency.create('KR', 0);

final Money amount = Money.parse(r'100', kr, 'S0');

final halfish = amount * 0.4;

print(halfish);

0

`The problem with the above equation is that `amount` has a scale of 0. As we don't know the scale of the double (0.4) we use the scale of the amount. The resuls in 0.4 being rounded down to zero.`

`The correct way to handle this is to first convert the double to a Fixed instance with the desired scale.`

import 'money2.dart';

/// 0 decimal place currency.

final Currency kr = Currency.create('KR', 0);

final Money amount = Money.parse(r'100', kr, 'S0');

final halfish = amount * Fixed.fromNum(0.4, scale: 1);

print(halfish);

40

`Money`

provides the following arithmetic operators:- unary
`-()`

`+(Money)`

`-(Money)`

`*(num)`

`/(num)`

**Operators**

`+`

**and**

`-`

**must be used with operands in same currency, otherwise**

`ArgumentError`

**will be thrown.**

import 'package:money2/money2.dart';

final tenDollars = fiveDollars + fiveDollars;

final zeroDollars = fiveDollars - fiveDollars;

Operators

`*`

, `/`

receive a `num`

as the second operand. Both operators use *schoolbook rounding*to round result up to a minorUnits of a currency.import 'package:money2/money2.dart';

final fifteenCents = Money.fromBigInt(BigInt.from(15), code: 'USD');

final thirtyCents = fifteenCents * 2; // $0.30

final eightCents = fifteenCents * 0.5; // $0.08 (rounded from 0.075)

Last modified 9mo ago