ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Round.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class Round
9 {
20  public static function round($number, $precision)
21  {
22  try {
23  $number = Helpers::validateNumericNullBool($number);
24  $precision = Helpers::validateNumericNullBool($precision);
25  } catch (Exception $e) {
26  return $e->getMessage();
27  }
28 
29  return round($number, (int) $precision);
30  }
31 
42  public static function up($number, $digits)
43  {
44  try {
45  $number = Helpers::validateNumericNullBool($number);
46  $digits = (int) Helpers::validateNumericNullSubstitution($digits, null);
47  } catch (Exception $e) {
48  return $e->getMessage();
49  }
50 
51  if ($number == 0.0) {
52  return 0.0;
53  }
54 
55  if ($number < 0.0) {
56  return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
57  }
58 
59  return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
60  }
61 
72  public static function down($number, $digits)
73  {
74  try {
75  $number = Helpers::validateNumericNullBool($number);
76  $digits = (int) Helpers::validateNumericNullSubstitution($digits, null);
77  } catch (Exception $e) {
78  return $e->getMessage();
79  }
80 
81  if ($number == 0.0) {
82  return 0.0;
83  }
84 
85  if ($number < 0.0) {
86  return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
87  }
88 
89  return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
90  }
91 
102  public static function multiple($number, $multiple)
103  {
104  try {
105  $number = Helpers::validateNumericNullSubstitution($number, 0);
106  $multiple = Helpers::validateNumericNullSubstitution($multiple, null);
107  } catch (Exception $e) {
108  return $e->getMessage();
109  }
110 
111  if ($number == 0 || $multiple == 0) {
112  return 0;
113  }
114  if ((Helpers::returnSign($number)) == (Helpers::returnSign($multiple))) {
115  $multiplier = 1 / $multiple;
116 
117  return round($number * $multiplier) / $multiplier;
118  }
119 
120  return Functions::NAN();
121  }
122 
139  public static function even($number)
140  {
141  try {
142  $number = Helpers::validateNumericNullBool($number);
143  } catch (Exception $e) {
144  return $e->getMessage();
145  }
146 
147  return Helpers::getEven($number);
148  }
149 
159  public static function odd($number)
160  {
161  try {
162  $number = Helpers::validateNumericNullBool($number);
163  } catch (Exception $e) {
164  return $e->getMessage();
165  }
166 
167  $significance = Helpers::returnSign($number);
168  if ($significance == 0) {
169  return 1;
170  }
171 
172  $result = ceil($number / $significance) * $significance;
173  if ($result == Helpers::getEven($result)) {
174  $result += $significance;
175  }
176 
177  return $result;
178  }
179 }
static validateNumericNullBool($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:27
static up($number, $digits)
ROUNDUP.
Definition: Round.php:42
$result
static validateNumericNullSubstitution($number, $substitute)
Validate numeric, but allow substitute for null.
Definition: Helpers.php:51
static multiple($number, $multiple)
MROUND.
Definition: Round.php:102
static down($number, $digits)
ROUNDDOWN.
Definition: Round.php:72
static round($number, $precision)
ROUND.
Definition: Round.php:20