Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Math.php
Go to the documentation of this file.
1 <?php
36 {
37  // support unit testing without using bcmath functions
38  public static $_bcmathDisabled = false;
39 
40  public static $add = array('Zend_Locale_Math', 'Add');
41  public static $sub = array('Zend_Locale_Math', 'Sub');
42  public static $pow = array('Zend_Locale_Math', 'Pow');
43  public static $mul = array('Zend_Locale_Math', 'Mul');
44  public static $div = array('Zend_Locale_Math', 'Div');
45  public static $comp = array('Zend_Locale_Math', 'Comp');
46  public static $sqrt = array('Zend_Locale_Math', 'Sqrt');
47  public static $mod = array('Zend_Locale_Math', 'Mod');
48  public static $scale = 'bcscale';
49 
50  public static function isBcmathDisabled()
51  {
53  }
54 
65  public static function round($op1, $precision = 0)
66  {
67  if (self::$_bcmathDisabled) {
68  $op1 = round($op1, $precision);
69  if (strpos((string) $op1, 'E') === false) {
70  return self::normalize(round($op1, $precision));
71  }
72  }
73 
74  if (strpos($op1, 'E') !== false) {
75  $op1 = self::floatalize($op1);
76  }
77 
78  $op1 = trim(self::normalize($op1));
79  $length = strlen($op1);
80  if (($decPos = strpos($op1, '.')) === false) {
81  $op1 .= '.0';
82  $decPos = $length;
83  $length += 2;
84  }
85  if ($precision < 0 && abs($precision) > $decPos) {
86  return '0';
87  }
88 
89  $digitsBeforeDot = $length - ($decPos + 1);
90  if ($precision >= ($length - ($decPos + 1))) {
91  return $op1;
92  }
93 
94  if ($precision === 0) {
95  $triggerPos = 1;
96  $roundPos = -1;
97  } elseif ($precision > 0) {
98  $triggerPos = $precision + 1;
99  $roundPos = $precision;
100  } else {
101  $triggerPos = $precision;
102  $roundPos = $precision -1;
103  }
104 
105  $triggerDigit = $op1[$triggerPos + $decPos];
106  if ($precision < 0) {
107  // zero fill digits to the left of the decimal place
108  $op1 = substr($op1, 0, $decPos + $precision) . str_pad('', abs($precision), '0');
109  }
110 
111  if ($triggerDigit >= '5') {
112  if ($roundPos + $decPos == -1) {
113  return str_pad('1', $decPos + 1, '0');
114  }
115 
116  $roundUp = str_pad('', $length, '0');
117  $roundUp[$decPos] = '.';
118  $roundUp[$roundPos + $decPos] = '1';
119 
120  if ($op1 > 0) {
121  if (self::$_bcmathDisabled) {
122  return Zend_Locale_Math_PhpMath::Add($op1, $roundUp, $precision);
123  }
124  return self::Add($op1, $roundUp, $precision);
125  } else {
126  if (self::$_bcmathDisabled) {
127  return Zend_Locale_Math_PhpMath::Sub($op1, $roundUp, $precision);
128  }
129  return self::Sub($op1, $roundUp, $precision);
130  }
131  } elseif ($precision >= 0) {
132  return substr($op1, 0, $decPos + ($precision ? $precision + 1: 0));
133  }
134 
135  return (string) $op1;
136  }
137 
144  public static function floatalize($value)
145  {
146  $value = strtoupper($value);
147  if (strpos($value, 'E') === false) {
148  return $value;
149  }
150 
151  $number = substr($value, 0, strpos($value, 'E'));
152  if (strpos($number, '.') !== false) {
153  $post = strlen(substr($number, strpos($number, '.') + 1));
154  $mantis = substr($value, strpos($value, 'E') + 1);
155  if ($mantis < 0) {
156  $post += abs((int) $mantis);
157  }
158 
159  $value = number_format($value, $post, '.', '');
160  } else {
161  $value = number_format($value, 0, '.', '');
162  }
163 
164  return $value;
165  }
166 
174  public static function normalize($value)
175  {
176  $convert = localeconv();
177  $value = str_replace($convert['thousands_sep'], "",(string) $value);
178  $value = str_replace($convert['positive_sign'], "", $value);
179  $value = str_replace($convert['decimal_point'], ".",$value);
180  if (!empty($convert['negative_sign']) and (strpos($value, $convert['negative_sign']))) {
181  $value = str_replace($convert['negative_sign'], "", $value);
182  $value = "-" . $value;
183  }
184 
185  return $value;
186  }
187 
195  public static function localize($value)
196  {
197  $convert = localeconv();
198  $value = str_replace(".", $convert['decimal_point'], (string) $value);
199  if (!empty($convert['negative_sign']) and (strpos($value, "-"))) {
200  $value = str_replace("-", $convert['negative_sign'], $value);
201  }
202  return $value;
203  }
204 
213  public static function exponent($value, $scale = null)
214  {
215  if (!extension_loaded('bcmath')) {
216  return $value;
217  }
218 
219  $split = explode('e', $value);
220  if (count($split) == 1) {
221  $split = explode('E', $value);
222  }
223 
224  if (count($split) > 1) {
225  $value = bcmul($split[0], bcpow(10, $split[1], $scale), $scale);
226  }
227 
228  return $value;
229  }
230 
239  public static function Add($op1, $op2, $scale = null)
240  {
241  $op1 = self::exponent($op1, $scale);
242  $op2 = self::exponent($op2, $scale);
243 
244  return bcadd($op1, $op2, $scale);
245  }
246 
255  public static function Sub($op1, $op2, $scale = null)
256  {
257  $op1 = self::exponent($op1, $scale);
258  $op2 = self::exponent($op2, $scale);
259  return bcsub($op1, $op2, $scale);
260  }
261 
270  public static function Pow($op1, $op2, $scale = null)
271  {
272  $op1 = self::exponent($op1, $scale);
273  $op2 = self::exponent($op2, $scale);
274  return bcpow($op1, $op2, $scale);
275  }
276 
285  public static function Mul($op1, $op2, $scale = null)
286  {
287  $op1 = self::exponent($op1, $scale);
288  $op2 = self::exponent($op2, $scale);
289  return bcmul($op1, $op2, $scale);
290  }
291 
300  public static function Div($op1, $op2, $scale = null)
301  {
302  $op1 = self::exponent($op1, $scale);
303  $op2 = self::exponent($op2, $scale);
304  return bcdiv($op1, $op2, $scale);
305  }
306 
314  public static function Sqrt($op1, $scale = null)
315  {
316  $op1 = self::exponent($op1, $scale);
317  return bcsqrt($op1, $scale);
318  }
319 
327  public static function Mod($op1, $op2)
328  {
329  $op1 = self::exponent($op1);
330  $op2 = self::exponent($op2);
331  return bcmod($op1, $op2);
332  }
333 
342  public static function Comp($op1, $op2, $scale = null)
343  {
344  $op1 = self::exponent($op1, $scale);
345  $op2 = self::exponent($op2, $scale);
346  return bccomp($op1, $op2, $scale);
347  }
348 }
349 
350 if (!extension_loaded('bcmath')
351  || (defined('TESTS_ZEND_LOCALE_BCMATH_ENABLED') && !TESTS_ZEND_LOCALE_BCMATH_ENABLED)
352 ) {
353  #require_once 'Zend/Locale/Math/PhpMath.php';
355 }
static $_bcmathDisabled
Definition: Math.php:38
static $sub
Definition: Math.php:41
static $comp
Definition: Math.php:45
static Sqrt($op1, $scale=null)
Definition: Math.php:314
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static localize($value)
Definition: Math.php:195
static Sub($op1, $op2, $scale=null)
Definition: PhpMath.php:80
static floatalize($value)
Definition: Math.php:144
static $sqrt
Definition: Math.php:46
$number
Definition: details.phtml:22
static $mul
Definition: Math.php:43
static exponent($value, $scale=null)
Definition: Math.php:213
static $add
Definition: Math.php:40
static Div($op1, $op2, $scale=null)
Definition: Math.php:300
static $mod
Definition: Math.php:47
static Comp($op1, $op2, $scale=null)
Definition: Math.php:342
static Add($op1, $op2, $scale=null)
Definition: PhpMath.php:57
$value
Definition: gender.phtml:16
static $div
Definition: Math.php:44
static isBcmathDisabled()
Definition: Math.php:50
static Mul($op1, $op2, $scale=null)
Definition: Math.php:285
static Mod($op1, $op2)
Definition: Math.php:327
static $scale
Definition: Math.php:48
static Sub($op1, $op2, $scale=null)
Definition: Math.php:255
static Pow($op1, $op2, $scale=null)
Definition: Math.php:270
static normalize($value)
Definition: Math.php:174
static round($op1, $precision=0)
Definition: Math.php:65
static Add($op1, $op2, $scale=null)
Definition: Math.php:239
static $pow
Definition: Math.php:42