| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
// following added in Php8.4 |
| 9 |
use RoundingMode; |
| 10 |
|
| 11 |
class Round |
| 12 |
{ |
| 13 |
use ArrayEnabled; |
| 14 |
|
| 15 |
/** |
| 16 |
* ROUND. |
| 17 |
* |
| 18 |
* Returns the result of builtin function round after validating args. |
| 19 |
* |
| 20 |
* @param mixed $number Should be numeric, or can be an array of numbers |
| 21 |
* @param mixed $precision Should be int, or can be an array of numbers |
| 22 |
* |
| 23 |
* @return array<mixed>|float|string Rounded number |
| 24 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 25 |
* with the same dimensions |
| 26 |
*/ |
| 27 |
public static function round($number, $precision = 0) |
| 28 |
{ |
| 29 |
if (is_array($number) || is_array($precision)) { |
| 30 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $precision); |
| 31 |
} |
| 32 |
|
| 33 |
try { |
| 34 |
$number = Helpers::validateNumericNullBool($number); |
| 35 |
$precision = Helpers::validateNumericNullBool($precision); |
| 36 |
} catch (Exception $e) { |
| 37 |
return $e->getMessage(); |
| 38 |
} |
| 39 |
|
| 40 |
return round($number, (int) $precision); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* ROUNDUP. |
| 45 |
* |
| 46 |
* Rounds a number up to a specified number of decimal places |
| 47 |
* |
| 48 |
* @param array<mixed>|float $number Number to round, or can be an array of numbers |
| 49 |
* @param array<mixed>|int $digits Number of digits to which you want to round $number, or can be an array of numbers |
| 50 |
* |
| 51 |
* @return array<mixed>|float|string Rounded Number, or a string containing an error |
| 52 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 53 |
* with the same dimensions |
| 54 |
*/ |
| 55 |
public static function up($number, $digits = 0) |
| 56 |
{ |
| 57 |
if (is_array($number) || is_array($digits)) { |
| 58 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $digits); |
| 59 |
} |
| 60 |
|
| 61 |
try { |
| 62 |
$number = Helpers::validateNumericNullBool($number); |
| 63 |
$digits = (int) Helpers::validateNumericNullSubstitution($digits, null); |
| 64 |
} catch (Exception $e) { |
| 65 |
return $e->getMessage(); |
| 66 |
} |
| 67 |
|
| 68 |
if ($number == 0.0) { |
| 69 |
return 0.0; |
| 70 |
} |
| 71 |
|
| 72 |
if (PHP_VERSION_ID >= 80400) { |
| 73 |
return round( |
| 74 |
(float) (string) $number, |
| 75 |
$digits, |
| 76 |
RoundingMode::AwayFromZero //* @phpstan-ignore argument.type (phpstan error till we no longer support Php < 8.4) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
// @codeCoverageIgnoreStart |
| 81 |
if ($number < 0.0) { |
| 82 |
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN); |
| 83 |
} |
| 84 |
|
| 85 |
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN); |
| 86 |
// @codeCoverageIgnoreEnd |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* ROUNDDOWN. |
| 91 |
* |
| 92 |
* Rounds a number down to a specified number of decimal places |
| 93 |
* |
| 94 |
* @param null|array<mixed>|float|string $number Number to round, or can be an array of numbers |
| 95 |
* @param array<mixed>|float|int|string $digits Number of digits to which you want to round $number, or can be an array of numbers |
| 96 |
* |
| 97 |
* @return array<mixed>|float|string Rounded Number, or a string containing an error |
| 98 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 99 |
* with the same dimensions |
| 100 |
*/ |
| 101 |
public static function down($number, $digits = 0) |
| 102 |
{ |
| 103 |
if (is_array($number) || is_array($digits)) { |
| 104 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $digits); |
| 105 |
} |
| 106 |
|
| 107 |
try { |
| 108 |
$number = Helpers::validateNumericNullBool($number); |
| 109 |
$digits = (int) Helpers::validateNumericNullSubstitution($digits, null); |
| 110 |
} catch (Exception $e) { |
| 111 |
return $e->getMessage(); |
| 112 |
} |
| 113 |
|
| 114 |
if ($number == 0.0) { |
| 115 |
return 0.0; |
| 116 |
} |
| 117 |
|
| 118 |
if (PHP_VERSION_ID >= 80400) { |
| 119 |
return round( |
| 120 |
(float) (string) $number, |
| 121 |
$digits, |
| 122 |
RoundingMode::TowardsZero |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
// @codeCoverageIgnoreStart |
| 127 |
if ($number < 0.0) { |
| 128 |
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP); |
| 129 |
} |
| 130 |
|
| 131 |
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP); |
| 132 |
// @codeCoverageIgnoreEnd |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* MROUND. |
| 137 |
* |
| 138 |
* Rounds a number to the nearest multiple of a specified value |
| 139 |
* |
| 140 |
* @param mixed $number Expect float. Number to round, or can be an array of numbers |
| 141 |
* @param mixed $multiple Expect int. Multiple to which you want to round, or can be an array of numbers. |
| 142 |
* |
| 143 |
* @return array<mixed>|float|int|string Rounded Number, or a string containing an error |
| 144 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 145 |
* with the same dimensions |
| 146 |
*/ |
| 147 |
public static function multiple($number, $multiple) |
| 148 |
{ |
| 149 |
if (is_array($number) || is_array($multiple)) { |
| 150 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $multiple); |
| 151 |
} |
| 152 |
|
| 153 |
try { |
| 154 |
$number = Helpers::validateNumericNullSubstitution($number, 0); |
| 155 |
$multiple = Helpers::validateNumericNullSubstitution($multiple, null); |
| 156 |
} catch (Exception $e) { |
| 157 |
return $e->getMessage(); |
| 158 |
} |
| 159 |
|
| 160 |
if ($number == 0 || $multiple == 0) { |
| 161 |
return 0; |
| 162 |
} |
| 163 |
if ((Helpers::returnSign($number)) == (Helpers::returnSign($multiple))) { |
| 164 |
$multiplier = 1 / $multiple; |
| 165 |
|
| 166 |
return round($number * $multiplier) / $multiplier; |
| 167 |
} |
| 168 |
|
| 169 |
return ExcelError::NAN(); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* EVEN. |
| 174 |
* |
| 175 |
* Returns number rounded up to the nearest even integer. |
| 176 |
* You can use this function for processing items that come in twos. For example, |
| 177 |
* a packing crate accepts rows of one or two items. The crate is full when |
| 178 |
* the number of items, rounded up to the nearest two, matches the crate's |
| 179 |
* capacity. |
| 180 |
* |
| 181 |
* Excel Function: |
| 182 |
* EVEN(number) |
| 183 |
* |
| 184 |
* @param array<mixed>|float $number Number to round, or can be an array of numbers |
| 185 |
* |
| 186 |
* @return array<mixed>|float|string Rounded Number, or a string containing an error |
| 187 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 188 |
* with the same dimensions |
| 189 |
*/ |
| 190 |
public static function even($number) |
| 191 |
{ |
| 192 |
if (is_array($number)) { |
| 193 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
| 194 |
} |
| 195 |
|
| 196 |
try { |
| 197 |
$number = Helpers::validateNumericNullBool($number); |
| 198 |
} catch (Exception $e) { |
| 199 |
return $e->getMessage(); |
| 200 |
} |
| 201 |
|
| 202 |
return Helpers::getEven($number); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* ODD. |
| 207 |
* |
| 208 |
* Returns number rounded up to the nearest odd integer. |
| 209 |
* |
| 210 |
* @param array<mixed>|float $number Number to round, or can be an array of numbers |
| 211 |
* |
| 212 |
* @return array<mixed>|float|int|string Rounded Number, or a string containing an error |
| 213 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 214 |
* with the same dimensions |
| 215 |
*/ |
| 216 |
public static function odd($number) |
| 217 |
{ |
| 218 |
if (is_array($number)) { |
| 219 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number); |
| 220 |
} |
| 221 |
|
| 222 |
try { |
| 223 |
$number = Helpers::validateNumericNullBool($number); |
| 224 |
} catch (Exception $e) { |
| 225 |
return $e->getMessage(); |
| 226 |
} |
| 227 |
|
| 228 |
$significance = Helpers::returnSign($number); |
| 229 |
if ($significance == 0) { |
| 230 |
return 1; |
| 231 |
} |
| 232 |
|
| 233 |
$result = ceil($number / $significance) * $significance; |
| 234 |
if ($result == Helpers::getEven($result)) { |
| 235 |
$result += $significance; |
| 236 |
} |
| 237 |
|
| 238 |
return $result; |
| 239 |
} |
| 240 |
} |
| 241 |
|