Calculator
Provides helpers for bcmath-based arithmetic.
The bcmath extension provides support for arbitrary precision arithmetic, which does not suffer from the precision loses that make floating point arithmetic unsafe for eCommerce.
Important: All numbers must be passed as strings.
- Full name:
\CommerceGuys\Intl\Calculator
- This class is marked as final and can't be subclassed
- This class is a Final class
Methods
add
Adds the second number to the first number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$first_number |
string | The first number. |
$second_number |
string | The second number. |
$scale |
int | The maximum number of digits after the decimal place. Any digit after $scale will be truncated. |
Return Value:
The result.
subtract
Subtracts the second number from the first number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$first_number |
string | The first number. |
$second_number |
string | The second number. |
$scale |
int | The maximum number of digits after the decimal place. Any digit after $scale will be truncated. |
Return Value:
The result.
multiply
Multiplies the first number by the second number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$first_number |
string | The first number. |
$second_number |
string | The second number. |
$scale |
int | The maximum number of digits after the decimal place. Any digit after $scale will be truncated. |
Return Value:
The result.
divide
Divides the first number by the second number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$first_number |
string | The first number. |
$second_number |
string | The second number. |
$scale |
int | The maximum number of digits after the decimal place. Any digit after $scale will be truncated. |
Return Value:
The result.
ceil
Calculates the next highest whole value of a number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$number |
string | The number. |
Return Value:
The result.
floor
Calculates the next lowest whole value of a number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$number |
string | The number. |
Return Value:
The result.
round
Rounds the given number.
Replicates PHP's support for rounding to the nearest even/odd number even if that number is decimal ($precision > 0).
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$number |
string | The number. |
$precision |
int | The number of decimals to round to. |
$mode |
int | The rounding mode. One of the following constants: PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, PHP_ROUND_HALF_ODD. |
Return Value:
The rounded number.
Throws:
compare
Compares the first number to the second number.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$first_number |
string | The first number. |
$second_number |
string | The second number. |
$scale |
int | The maximum number of digits after the decimal place. Any digit after $scale will be truncated. |
Return Value:
0 if both numbers are equal, 1 if the first one is greater, -1 otherwise.
trim
Trims the given number.
By default bcmath returns numbers with the number of digits according to $scale. This means that bcadd('2', '2', 6) will return '4.00000'. Trimming the number removes the excess zeroes.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$number |
string | The number to trim. |
Return Value:
The trimmed number.
assertNumberFormat
Assert that the given number is a numeric string value.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$number |
string | The number to check. |
Throws:
Automatically generated on 2025-03-18