PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / MathTrig / Ceiling.php

Ceiling.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php

197 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Functions;
8 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
10 class Ceiling
11 {
12 use ArrayEnabled;
13
14 /**
15 * CEILING.
16 *
17 * Returns number rounded up, away from zero, to the nearest multiple of significance.
18 * For example, if you want to avoid using pennies in your prices and your product is
19 * priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the
20 * nearest nickel.
21 *
22 * Excel Function:
23 * CEILING(number[,significance])
24 *
25 * @param array<mixed>|float $number the number you want the ceiling
26 * Or can be an array of values
27 * @param array<mixed>|float $significance the multiple to which you want to round
28 * Or can be an array of values
29 *
30 * @return array<mixed>|float|string Rounded Number, or a string containing an error
31 * If an array of numbers is passed as an argument, then the returned result will also be an array
32 * with the same dimensions
33 */
34 public static function ceiling($number, $significance = null)
35 {
36 if (is_array($number) || is_array($significance)) {
37 return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
38 }
39
40 /*
41 // Allow one argument only, for compatibility with Google Sheets, when importing an Excel file.
42 if ($significance === null) {
43 self::floorCheck1Arg();
44 }
45 */
46
47 try {
48 $number = Helpers::validateNumericNullBool($number);
49 $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
50 } catch (Exception $e) {
51 return $e->getMessage();
52 }
53
54 return self::argumentsOk((float) $number, (float) $significance);
55 }
56
57 /**
58 * CEILING.MATH.
59 *
60 * Round a number down to the nearest integer or to the nearest multiple of significance.
61 *
62 * Excel Function:
63 * CEILING.MATH(number[,significance[,mode]])
64 *
65 * @param mixed $number Number to round
66 * Or can be an array of values
67 * @param mixed $significance Significance
68 * Or can be an array of values
69 * @param array<mixed>|int $mode direction to round negative numbers
70 * Or can be an array of values
71 *
72 * @return array<mixed>|float|string Rounded Number, or a string containing an error
73 * If an array of numbers is passed as an argument, then the returned result will also be an array
74 * with the same dimensions
75 */
76 public static function math($number, $significance = null, $mode = 0, bool $checkSigns = false)
77 {
78 if (is_array($number) || is_array($significance) || is_array($mode)) {
79 return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
80 }
81
82 try {
83 $number = Helpers::validateNumericNullBool($number);
84 $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
85 $mode = Helpers::validateNumericNullSubstitution($mode, null);
86 } catch (Exception $e) {
87 return $e->getMessage();
88 }
89
90 if (empty($significance * $number)) {
91 return 0.0;
92 }
93 if ($checkSigns) {
94 if (($number > 0 && $significance < 0) || ($number < 0 && $significance > 0)) {
95 return ExcelError::NAN();
96 }
97 }
98 if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) {
99 return floor($number / $significance) * $significance;
100 }
101
102 return ceil($number / $significance) * $significance;
103 }
104
105 /**
106 * CEILING.PRECISE.
107 *
108 * Rounds number up, away from zero, to the nearest multiple of significance.
109 *
110 * Excel Function:
111 * CEILING.PRECISE(number[,significance])
112 *
113 * @param mixed $number the number you want to round
114 * Or can be an array of values
115 * @param array<mixed>|float $significance the multiple to which you want to round
116 * Or can be an array of values
117 *
118 * @return array<mixed>|float|string Rounded Number, or a string containing an error
119 * If an array of numbers is passed as an argument, then the returned result will also be an array
120 * with the same dimensions
121 */
122 public static function precise($number, $significance = 1)
123 {
124 if (is_array($number) || is_array($significance)) {
125 return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
126 }
127
128 try {
129 $number = Helpers::validateNumericNullBool($number);
130 $significance = Helpers::validateNumericNullSubstitution($significance, null);
131 } catch (Exception $e) {
132 return $e->getMessage();
133 }
134
135 if (!$significance) {
136 return 0.0;
137 }
138 $result = $number / abs($significance);
139
140 return ceil($result) * $significance * (($significance < 0) ? -1 : 1);
141 }
142
143 /**
144 * CEILING.ODS, pseudo-function - CEILING as implemented in ODS.
145 *
146 * ODS Function (theoretical):
147 * CEILING.ODS(number[,significance[,mode]])
148 *
149 * @param mixed $number Number to round
150 * @param mixed $significance Significance
151 * @param array<mixed>|int $mode direction to round negative numbers
152 *
153 * @return array<mixed>|float|string Rounded Number, or a string containing an error
154 */
155 public static function mathOds($number, $significance = null, $mode = 0)
156 {
157 return self::math($number, $significance, $mode, true);
158 }
159
160 /**
161 * Let CEILINGMATH complexity pass Scrutinizer.
162 */
163 private static function ceilingMathTest(float $significance, float $number, int $mode): bool
164 {
165 return ($significance < 0) || ($number < 0 && !empty($mode));
166 }
167
168 /**
169 * Avoid Scrutinizer problems concerning complexity.
170 * @return float|string
171 */
172 private static function argumentsOk(float $number, float $significance)
173 {
174 if (empty($number * $significance)) {
175 return 0.0;
176 }
177 $signSig = Helpers::returnSign($significance);
178 $signNum = Helpers::returnSign($number);
179 if (
180 ($signSig === 1 && ($signNum === 1 || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_GNUMERIC))
181 || ($signSig === -1 && $signNum === -1)
182 ) {
183 return ceil($number / $significance) * $significance;
184 }
185
186 return ExcelError::NAN();
187 }
188
189 private static function floorCheck1Arg(): void
190 {
191 $compatibility = Functions::getCompatibilityMode();
192 if ($compatibility === Functions::COMPATIBILITY_EXCEL) {
193 throw new Exception('Excel requires 2 arguments for CEILING');
194 }
195 }
196 }
197