# Arithmetic Operations

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

Whilst money does support a number of arithmetic operations that take a double as the operator this is STRONGLY DISCOURAGE.  Doubles are imprecise and will introduce rounding errors.

Where ever possible used the methods that take a Fixed, Int or BigInt.

### MultiplyByFixed

Multiply a Money instance by a Fixed instance.

```dart
final kr = Currency.create('KR', 0);
final amount = Money.parseWithCurrency('100', kr, pattern: 'S0');
final halfish = amount.multiplyByFixed(Fixed.parse('0.4', ));
expect(halfish, equals(Money.fromIntWithCurrency(40, kr)));
expect(halfish.decimalDigits, equals(0));
```

### Multiply by num (double or int)

The '\*' operator takes a num and converts it to a Fixed value with 16 decimal places. However the scale of the result is dictated by the Money instance passed to the '\*' operator.

```dart
/// Create the KR currency with 0 decimal digits.
final kr = Currency.create('KR', 0);
/// parse '100' to get a Money storing KR100.
final amount = Money.parseWithCurrency('100', kr, pattern: 'S0');
/// Multiply by 0.4 to get KR40
final halfish = amount * 0.4;
expect(halfish, equals(Money.fromIntWithCurrency(40, kr)));
/// scale of the results is 0 as dictated by the Currency.
expect(halfish.decimalDigits, equals(0));
```

`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.**

```dart
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.

```dart
import 'package:money2/money2.dart';
    test('Operators', () {
      final fiveDollars = Money.parse('5.00', isoCode: 'USD');
      final tenDollars = fiveDollars + fiveDollars;
      expect(tenDollars.minorUnits.toInt(), equals(1000));
      final zeroDollars = fiveDollars - fiveDollars;
      expect(zeroDollars.minorUnits.toInt(), equals(0));

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

      final thirtyCents = fifteenCents * 2; // $0.30
      expect(thirtyCents.minorUnits.toInt(), equals(30));
      final eightCents = fifteenCents * 0.5; // $0.08 (rounded from 0.075)
      expect(eightCents.minorUnits.toInt(), equals(8));
    });
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://money2.onepub.dev/arithmetic-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
