Money class is the primary class for working with monetary values in Laravel Brick Money. It wraps the Brick\Money\Money class and provides a fluent Laravel interface.
Class Definition
Macroable trait, allowing you to extend it with custom methods.
Creating Money Instances
of()
of()
of()
Returns Money of the given amount and currency.By default, the money is created with aDefaultContext. This means that the amount is scaled to match the currency’s default fraction digits. For example, Money::of('2.5', 'USD') will yield USD 2.50. If the amount cannot be safely converted to this scale, an exception is thrown.To override this behaviour, a Context instance can be provided. Operations on this Money return a Money with the same context.The monetary amount
The currency code (e.g., ‘USD’) or Currency instance
Optional context for precision control
Rounding mode to use if rounding is required
A new Money instance
ofMinor()
ofMinor()
ofMinor()
Returns Money from a number of minor units (cents).By default, the money is created with aDefaultContext. This means that the amount is scaled to match the currency’s default fraction digits. For example, Money::ofMinor('1234', 'USD') will yield USD 12.34. If the amount cannot be safely converted to this scale, an exception is thrown.The amount in minor units (cents)
The currency code (e.g., ‘USD’) or Currency instance
Optional context for precision control
Rounding mode to use if rounding is required
A new Money instance
ofMoney()
ofMoney()
Configuration Methods
locale()
locale()
jsonSerializer()
jsonSerializer()
Getter Methods
getMoney()
getMoney()
getCurrency()
getCurrency()
getContext()
getContext()
getAmount()
getAmount()
getMinorAmount()
getMinorAmount()
getMinorAmount()
Returns the amount of this Money in minor units (cents) for the currency.The value is returned as a BigDecimal. If this Money has a scale greater than that of the currency, the result will have a non-zero scale.For example,USD 1.23 will return a BigDecimal of 123, while USD 1.2345 will return 123.45.The amount in minor units
getUnscaledAmount()
getUnscaledAmount()
getSign()
getSign()
Comparison Methods
isZero()
isZero()
isNegative()
isNegative()
isNegativeOrZero()
isNegativeOrZero()
isPositive()
isPositive()
isPositiveOrZero()
isPositiveOrZero()
compareTo()
compareTo()
isEqualTo()
isEqualTo()
isLessThan()
isLessThan()
isLessThanOrEqualTo()
isLessThanOrEqualTo()
isGreaterThan()
isGreaterThan()
isGreaterThanOrEqualTo()
isGreaterThanOrEqualTo()
Arithmetic Operations
plus()
plus()
plus()
Returns the sum of this Money and the given amount.If the operand is a Money, it must have the same context as this Money, or an exception is thrown. This is by design, to ensure that contexts are not mixed accidentally.The resulting Money has the same context as this Money. If the result needs rounding to fit this context, a rounding mode can be provided. If a rounding mode is not provided and rounding is necessary, an exception is thrown.The amount to add
Rounding mode if rounding is required
A new Money instance with the sum
minus()
minus()
minus()
Returns the difference of this Money and the given amount.If the operand is a Money, it must have the same context as this Money, or an exception is thrown. This is by design, to ensure that contexts are not mixed accidentally.The resulting Money has the same context as this Money. If the result needs rounding to fit this context, a rounding mode can be provided. If a rounding mode is not provided and rounding is necessary, an exception is thrown.The amount to subtract
Rounding mode if rounding is required
A new Money instance with the difference
multipliedBy()
multipliedBy()
multipliedBy()
Returns the product of this Money and the given number.The resulting Money has the same context as this Money. If the result needs rounding to fit this context, a rounding mode can be provided. If a rounding mode is not provided and rounding is necessary, an exception is thrown.The multiplier
Rounding mode if rounding is required
A new Money instance with the product
dividedBy()
dividedBy()
dividedBy()
Returns the result of the division of this Money by the given number.The resulting Money has the same context as this Money. If the result needs rounding to fit this context, a rounding mode can be provided. If a rounding mode is not provided and rounding is necessary, an exception is thrown.The divisor
Rounding mode if rounding is required
A new Money instance with the quotient
quotient()
quotient()
quotient()
Returns the quotient of the division of this Money by the given number.The given number must be an integer value. The resulting Money has the same context as this Money. This method can serve as a basis for a money allocation algorithm.The divisor (must be integer)
The quotient
quotientAndRemainder()
quotientAndRemainder()
quotientAndRemainder()
Returns the quotient and the remainder of the division of this Money by the given number.The given number must be an integer value. The resulting monies have the same context as this Money. This method can serve as a basis for a money allocation algorithm.The divisor (must be integer)
An array containing [quotient, remainder]
Allocation Methods
allocate()
allocate()
allocate()
Allocates this Money according to a list of ratios.If the allocation yields a remainder, its amount is split over the first monies in the list, so that the total of the resulting monies is always equal to this Money.For example, given aUSD 49.99 money in the default context, allocate(1, 2, 3, 4) returns [USD 5.00, USD 10.00, USD 15.00, USD 19.99]The resulting monies have the same context as this Money.Variable number of ratio integers
Array of allocated Money instances
allocateWithRemainder()
allocateWithRemainder()
allocateWithRemainder()
Allocates this Money according to a list of ratios.The remainder is also present, appended at the end of the list.For example, given aUSD 49.99 money in the default context, allocateWithRemainder(1, 2, 3, 4) returns [USD 4.99, USD 9.98, USD 14.97, USD 19.96, USD 0.09]The resulting monies have the same context as this Money.Variable number of ratio integers
Array of allocated Money instances with remainder
split()
split()
split()
Splits this Money into a number of parts.If the division of this Money by the number of parts yields a remainder, its amount is split over the first monies in the list, so that the total of the resulting monies is always equal to this Money.For example, given aUSD 100.00 money in the default context, split(3) returns [USD 33.34, USD 33.33, USD 33.33]The resulting monies have the same context as this Money.Number of parts to split into
Array of split Money instances
splitWithRemainder()
splitWithRemainder()
splitWithRemainder()
Splits this Money into a number of parts and a remainder.For example, given aUSD 100.00 money in the default context, splitWithRemainder(3) returns [USD 33.33, USD 33.33, USD 33.33, USD 0.01]The resulting monies have the same context as this Money.Number of parts to split into
Array of split Money instances with remainder
Transformation Methods
abs()
abs()
negated()
negated()
convertedTo()
convertedTo()
convertedTo()
Converts this Money to another currency, using an exchange rate.By default, the resulting Money has the same context as this Money. This can be overridden by providing a Context.For example, converting a default money ofUSD 1.23 to EUR with an exchange rate of 0.91 and RoundingMode::UP will yield EUR 1.12.The target currency
The exchange rate to apply
Optional context for the result
Rounding mode if rounding is required
A new Money instance in the target currency
Formatting Methods
format()
format()
format()
Formats this Money.Note that this method usesnumber_format, which internally represents values using floating point arithmetic, so discrepancies can appear when formatting very large monetary values.If true, whole numbers are formatted without decimal places
The formatted money string
formatLocale()
formatLocale()
formatLocale()
Formats this Money with the given locale.Note that this method usesNumberFormatter, which internally represents values using floating point arithmetic, so discrepancies can appear when formatting very large monetary values.The locale to use (defaults to Money::locale())
If true, whole numbers are formatted without decimal places
Optional callback to customize the NumberFormatter
The formatted money string
Serialization Methods
toArray()
toArray()
toJson()
toJson()
jsonSerialize()
jsonSerialize()
__toString()
__toString()
Macroable
The Money class uses Laravel’sMacroable trait, allowing you to add custom methods: