Skip to content

Commit

Permalink
Add note about rounding mode in of()
Browse files Browse the repository at this point in the history
See #87.
  • Loading branch information
BenMorel authored Mar 26, 2024
1 parent a6712c0 commit d88c3c6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ If you need to upgrade to a newer release cycle, check the [release history](htt

## Creating a Money

### From a regular currency value

To create a Money, call the `of()` factory method:

```php
Expand All @@ -55,6 +57,17 @@ $money = Money::of(50, 'USD'); // USD 50.00
$money = Money::of('19.9', 'USD'); // USD 19.90
```

If the given amount does not fit in the currency's default number of decimal places (2 for `USD`), you can pass a `RoundingMode`:

```php
$money = Money::of('123.456', 'USD'); // RoundingNecessaryException
$money = Money::of('123.456', 'USD', roundingMode: RoundingMode::UP); // USD 123.46
```

**Note that the rounding mode is only used once**, for the value provided in `of()`; it is not stored in the `Money` object, and any subsequent operation will still need to be passed a `RoundingMode` when necessary.

### From minor units (cents)

Alternatively, you can create a Money from a number of "minor units" (cents), using the `ofMinor()` method:

```php
Expand Down

0 comments on commit d88c3c6

Please sign in to comment.