{"id":61,"date":"2019-08-30T19:30:32","date_gmt":"2019-08-30T19:30:32","guid":{"rendered":"https:\/\/tutorialio.com\/?p=61"},"modified":"2019-08-30T19:30:32","modified_gmt":"2019-08-30T19:30:32","slug":"numerical-functions-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/numerical-functions-in-php\/","title":{"rendered":"Numerical Functions in PHP"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>Numerical Functions in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#abs\">abs()<\/a><\/td>\n<td>Absolute value<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#ceil\">ceil()<\/a><\/td>\n<td>Round fractions up<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#floor\">floor()<\/a><\/td>\n<td>Round fractions down<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#fmod\">fmod()<\/a><\/td>\n<td>Returns the floating point remainder (modulo) of the division of the arguments<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#isfinite\">is_finite()<\/a><\/td>\n<td>Finds whether a value is a legal finite number<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#isinfinite\">is_infinite()<\/a><\/td>\n<td>Finds whether a value is infinite<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#isnan\">is_nan()<\/a><\/td>\n<td>Finds whether a value is not a number<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#max\">max()<\/a><\/td>\n<td>Find highest value<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#min\">min()<\/a><\/td>\n<td>Find lowest value<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#round\">round()<\/a><\/td>\n<td>Rounds a float<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"abs\">PHP <code>abs()<\/code> Function<\/h2>\n<h3>PHP <code>abs()<\/code> Usage<\/h3>\n<p>The PHP <code><code>abs()<\/code><\/code> function will give you the absolute value of <code class=\"parameter\">number<\/code>.<\/p>\n<h3>PHP <code>abs()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> abs ( mixed $number ) : number<\/code><\/pre>\n<h3>PHP <code>abs()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 The numeric value to process <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>abs()<\/code> Return Value<\/h3>\n<p>The PHP <code>abs()<\/code> function returns type is also float, otherwise it is integer (as float usually has a bigger value range than integer).<\/p>\n<h3>PHP <code>abs()<\/code> Working Examples<\/h3>\n<h4>1. abs() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho abs(-4.2); \/\/ 4.2 (double\/float)\necho abs(5);    \/\/ 5 (integer)\necho abs(-5);   \/\/ 5 (integer)\n?&gt;<\/code><\/pre>\n<h2 id=\"ceil\">PHP <code>ceil()<\/code> Function<\/h2>\n<h3>PHP <code>ceil()<\/code> Usage<\/h3>\n<p>The PHP <code><code>ceil()<\/code><\/code> function will give you the next highest integer value by rounding up <code class=\"parameter\">value<\/code> if necessary.<\/p>\n<h3>PHP <code>ceil()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> ceil ( float $value ) : float<\/code><\/pre>\n<h3>PHP <code>ceil()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>value<\/code> \u2014 The value to round <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>ceil()<\/code> Return Value<\/h3>\n<p>The PHP <code>ceil()<\/code> function returns value of <code>ceil()<\/code> is still of type float as the value range of float is usually bigger than that of integer.<\/p>\n<h3>PHP <code>ceil()<\/code> Working Examples<\/h3>\n<h4>1. ceil() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho ceil(4.3);    \/\/ 5\necho ceil(9.999);  \/\/ 10\necho ceil(-3.14);  \/\/ -3\n?&gt;<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: Scott Weaver \/ scottmweaver * gmail<\/p>\n<pre><code>I needed this and couldn't find it so I thought someone else wouldn't have to look through a bunch of Google results-\n<br>&lt;?php\n\/\/ duplicates m$ excel's ceiling function\nif( !function_exists('ceiling') )\n{\n    function ceiling($number, $significance = 1)\n    {\n        return ( is_numeric($number) &amp;&amp; is_numeric($significance) ) ? (ceil($number\/$significance)*$significance) : false;\n    }\n}\necho ceiling(0, 1000);     \/\/ 0\necho ceiling(1, 1);        \/\/ 1000\necho ceiling(1001, 1000);  \/\/ 2000\necho ceiling(1.27, 0.05);  \/\/ 1.30\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: eep2004<\/p>\n<pre><code>Caution!\n<br>&lt;?php\n$value = 77.4;\necho ceil($value * 100) \/ 100;         \/\/ 77.41 - WRONG!\necho ceil(round($value * 100)) \/ 100;  \/\/ 77.4 - OK!<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: steve_phpnet \/\/ nanovox \\\\ com<\/p>\n<pre><code>I couldn't find any functions to do what ceiling does while still leaving I specified number of decimal places, so I wrote a couple functions myself.  round_up is like ceil but allows you to specify a number of decimal places.  round_out does the same, but rounds away from zero.\n<br>&lt;?php\n \/\/ round_up:\n \/\/ rounds up a float to a specified number of decimal places\n \/\/ (basically acts like ceil() but allows for decimal places)\n function round_up ($value, $places=0) {\n  if ($places &lt; 0) { $places = 0; }\n  $mult = pow(10, $places);\n  return ceil($value * $mult) \/ $mult;\n }\n \/\/ round_out:\n \/\/ rounds a float away from zero to a specified number of decimal places\n function round_out ($value, $places=0) {\n  if ($places &lt; 0) { $places = 0; }\n  $mult = pow(10, $places);\n  return ($value &gt;= 0 ? ceil($value * $mult):floor($value * $mult)) \/ $mult;\n }\n echo round_up (56.77001, 2); \/\/ displays 56.78\n echo round_up (-0.453001, 4); \/\/ displays -0.453\n echo round_out (56.77001, 2); \/\/ displays 56.78\n echo round_out (-0.453001, 4); \/\/ displays -0.4531\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"floor\">PHP <code>floor()<\/code> Function<\/h2>\n<h3>PHP <code>floor()<\/code> Usage<\/h3>\n<p>The PHP <code><code>floor()<\/code><\/code> function will give you the next lowest integer value (as float) by rounding down <code class=\"parameter\">value<\/code> if necessary.<\/p>\n<h3>PHP <code>floor()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> floor ( float $value ) : float<\/code><\/pre>\n<h3>PHP <code>floor()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>value<\/code> \u2014 The numeric value to round <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>floor()<\/code> Return Value<\/h3>\n<p>The PHP <code>floor()<\/code> function returns value of <code>floor()<\/code> is still of type float because the value range of float is usually bigger than that of integer. This function return <code>FALSE<\/code> in case of an error (e.g. passing an array).<\/p>\n<h3>PHP <code>floor()<\/code> Working Examples<\/h3>\n<h4>1. floor() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho floor(4.3);   \/\/ 4\necho floor(9.999); \/\/ 9\necho floor(-3.14); \/\/ -4\n?&gt;<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: greene dot mc<\/p>\n<pre><code>I believe this behavior of the floor function was intended.  Note that it says \"the next lowest integer\".  -1 is \"higher\" than -1.6.  As in, -1 is logically greater than -1.6.  To go lower the floor function would go to -2 which is logically less than -1.6.\nFloor isn't trying to give you the number closest to zero, it's giving you the lowest bounding integer of a float.\nIn reply to Glen who commented:\n Glen\n01-Dec-2007 04:22\n<br>&lt;?php\n  echo floor(1.6);  \/\/ will output \"1\"\n  echo floor(-1.6); \/\/ will output \"-2\"\n?&gt;<br>\ninstead use intval (seems to work v5.1.6):\n<br>&lt;?php\n  echo intval(1.6);  \/\/ will output \"1\"\n  echo intval(-1.6); \/\/ will output \"-1\"\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: seppili_<\/p>\n<pre><code>I use this function to floor with decimals:\n<br>&lt;?php\nfunction floordec($zahl,$decimals=2){    \n     return floor($zahl*pow(10,$decimals))\/pow(10,$decimals);\n}\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: maikl<\/p>\n<pre><code>A correction to the funcion floor_dec from the user \"php is the best\".\nIf the number is 0.05999 it returns 0.59 because the zero at left position is deleted.\nI just added a '1' and after the floor or ceil call remove with a substr.\nHope it helps.\nfunction floor_dec($number,$precision = 2,$separator = '.') {\n  $numberpart=explode($separator,$number);\n  $numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);\n  if($numberpart[0]&gt;=0) {\n    $numberpart[1]=substr(floor('1'.$numberpart[1]),1);\n  } else {\n    $numberpart[1]=substr(ceil('1'.$numberpart[1]),1);\n  }\n  $ceil_number= array($numberpart[0],$numberpart[1]);\n  return implode($separator,$ceil_number);\n}<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: benrr101<\/p>\n<pre><code>But, if you want the number closest to zero, you could use this:\n<br>&lt;?php\n  if($foo &gt; 0) {\n    floor($foo);\n  } else {\n    ceil($foo);\n  }\n?&gt;<br>\n-benrr101<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: fragov<\/p>\n<pre><code>Have solved a \"price problem\": \n \n<br>&lt;?php \n$peny = floor($row-&gt;price*1000) - floor($row-&gt;price)*1000)\/10; \n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"fmod\">PHP <code>fmod()<\/code> Function<\/h2>\n<h3>PHP <code>fmod()<\/code> Usage<\/h3>\n<p>The PHP <code><code>fmod()<\/code><\/code> function will give you the floating point remainder (modulo) of the division of the arguments.<\/p>\n<h3>PHP <code>fmod()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> fmod ( float $x , float $y ) : float<\/code><\/pre>\n<h3>PHP <code>fmod()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>x<\/code> \u2014 The dividend <\/p>\n<\/li>\n<li>\n<p><code>y<\/code> \u2014 The divisor <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>fmod()<\/code> Return Value<\/h3>\n<p>The PHP <code>fmod()<\/code> function returns the floating point remainder of <code class=\"parameter\">x<\/code>\/<code class=\"parameter\">y<\/code><\/p>\n<h3>PHP <code>fmod()<\/code> Working Examples<\/h3>\n<h4>1. Using fmod()<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$x = 5.7;\n$y = 1.3;\n$r = fmod($x, $y);\n\/\/ $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7\n?&gt;<\/code><\/pre>\n<h2 id=\"is_finite\">PHP <code>is_finite()<\/code> Function<\/h2>\n<h3>PHP <code>is_finite()<\/code> Usage<\/h3>\n<p>The PHP <code><code>is_finite()<\/code><\/code> function will finds whether a value is a legal finite number.<\/p>\n<h3>PHP <code>is_finite()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> is_finite ( float $val ) : bool<\/code><\/pre>\n<h3>PHP <code>is_finite()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>val<\/code> \u2014 The value to check <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>is_finite()<\/code> Return Value<\/h3>\n<p>The PHP <code>is_finite()<\/code> function returns <code>TRUE<\/code> if <code class=\"parameter\">val<\/code> is a legal finite number within the allowed range for a PHP float on this platform, else <code>FALSE<\/code>.<\/p>\n<h2 id=\"is_infinite\">PHP <code>is_infinite()<\/code> Function<\/h2>\n<h3>PHP <code>is_infinite()<\/code> Usage<\/h3>\n<p>The PHP <code><code>is_infinite()<\/code><\/code> function will give you <code>TRUE<\/code> if <code class=\"parameter\">val<\/code> is infinite (positive or negative), like the result of <strong>log(0)<\/strong> or any value too big to fit into a float on this platform.<\/p>\n<h3>PHP <code>is_infinite()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> is_infinite ( float $val ) : bool<\/code><\/pre>\n<h3>PHP <code>is_infinite()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>val<\/code> \u2014 The value to check <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>is_infinite()<\/code> Return Value<\/h3>\n<p>The PHP <code>is_infinite()<\/code> function returns <code>TRUE<\/code> if <code class=\"parameter\">val<\/code> is infinite, else <code>FALSE<\/code>.<\/p>\n<h2 id=\"is_nan\">PHP <code>is_nan()<\/code> Function<\/h2>\n<h3>PHP <code>is_nan()<\/code> Usage<\/h3>\n<p>The PHP <code><code>is_nan()<\/code><\/code> function will checks whether <code class=\"parameter\">val<\/code> is 'not a number', like the result of <strong>acos(1.01)<\/strong>.<\/p>\n<h3>PHP <code>is_nan()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> is_nan ( float $val ) : bool<\/code><\/pre>\n<h3>PHP <code>is_nan()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>val<\/code> \u2014 The value to check <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>is_nan()<\/code> Return Value<\/h3>\n<p>The PHP <code>is_nan()<\/code> function returns <code>TRUE<\/code> if <code class=\"parameter\">val<\/code> is 'not a number', else <code>FALSE<\/code>.<\/p>\n<h3>PHP <code>is_nan()<\/code> Working Examples<\/h3>\n<h4>1. is_nan() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/\/ Invalid calculation, will return a \n\/\/ NaN value\n$nan = acos(8);\nvar_dump($nan, is_nan($nan));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">float(NAN)\nbool(true)<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: darkangel<\/p>\n<pre><code>nan\/\"not a number\" is not meant to see if the data type is numeric\/textual\/etc..\nNaN is actually a set of values which can be stored in floating-point variables, but dont actually evaluate to a proper floating point number.\nThe floating point system has three sections: 1 bit for the sign (+\/-), an 8 bit exponent, and a 23 bit fractional part.\nThere are rules governing which combinations of values can be placed into each section, and some values are reserved for numbers such as infinity. This leads to certain combinations being invalid, or in other words, not a number.<\/pre>\n<p><\/code><\/p>\n<h2 id=\"max\">PHP <code>max()<\/code> Function<\/h2>\n<h3>PHP <code>max()<\/code> Usage<\/h3>\n<p>The PHP <code><code>max()<\/code><\/code> function will give you the highest value in that array. If at least two parameters are provided, <code>max()<\/code> return the biggest of these values.<\/p>\n<h3>PHP <code>max()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> max ( array $values ) : mixed<\/code><\/pre>\n<pre><code class=\"lang-php\"> max ( mixed $value1 [, mixed $... ] ) : mixed<\/code><\/pre>\n<h3>PHP <code>max()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>values<\/code> \u2014 An array containing the values. <\/p>\n<\/li>\n<li>\n<p><code>value1<\/code> \u2014 Any comparable value. <\/p>\n<\/li>\n<li>\n<p><code>...<\/code> \u2014 Any comparable value. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>max()<\/code> Return Value<\/h3>\n<p>The PHP <code>max()<\/code> function returns the parameter value considered \"highest\" according to standard comparisons. If multiple values of different types evaluate as equal (e.g. <strong>0<\/strong> and <strong>'abc'<\/strong>) the first provided to the function will be returned.<\/p>\n<h3>PHP <code>max()<\/code> Working Examples<\/h3>\n<h4>1. Example uses of max()<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho max(2, 3, 1, 6, 7);  \/\/ 7\necho max(array(2, 4, 5)); \/\/ 5\n\/\/ The string 'hello' when compared to an int is treated as 0\n\/\/ Since the two values are equal, the order they are provided determines the result\necho max(0, 'hello');     \/\/ 0\necho max('hello', 0);     \/\/ hello\n\/\/ Here we are comparing -1 &lt; 0, so 'hello' is the highest value\necho max('hello', -1);    \/\/ hello\n\/\/ With multiple arrays of different lengths, max returns the longest\n$val = max(array(2, 2, 2), array(1, 1, 1, 1)); \/\/ array(1, 1, 1, 1)\n\/\/ Multiple arrays of the same length are compared from left to right\n\/\/ so in our example: 2 == 2, but 5 &gt; 4\n$val = max(array(2, 4, 8), array(2, 5, 1)); \/\/ array(2, 5, 1)\n\/\/ If both an array and non-array are given, the array will be returned\n\/\/ as comparisons treat arrays as greater than any other value\n$val = max('string', array(2, 5, 7), 42);   \/\/ array(2, 5, 7)\n\/\/ If one argument is NULL or a boolean, it will be compared against\n\/\/ other values using the rule FALSE &lt; TRUE regardless of the other types involved\n\/\/ In the below example, -10 is treated as TRUE in the comparison\n$val = max(-10, FALSE); \/\/ -10\n\/\/ 0, on the other hand, is treated as FALSE, so is \"lower than\" TRUE\n$val = max(0, TRUE); \/\/ TRUE\n?&gt;<\/code><\/pre>\n<h3>Important Points about PHP <code>max()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> Values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an integer as though it were <strong>0<\/strong>, but multiple non-numeric string values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied. <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: keith<\/p>\n<pre><code>The simplest way to get around the fact that max() won't give the key is array_search:\n<br>&lt;?php\n$student_grades = array (\"john\" =&gt; 100, \"sarah\" =&gt; 90, \"anne\" =&gt; 100);\n$top_student = array_search(max($student_grades),$student_grades); \/\/ john\n?&gt;<br>\nThis could also be done with array_flip, though overwriting will mean that it gets the last max value rather than the first:\n<br>&lt;?php\n$grades_index = array_flip($student_grades);\n$top_student = $grades_index[max($student_grades)]; \/\/ anne\n?&gt;<br>\nTo get all the max value keys:\n<br>&lt;?php\n$top_students = array_keys($student_grades,max($student_grades)); \/\/ john, anne\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: costinu<\/p>\n<pre><code>max(null, 0) = null \nmax(0, null) = 0<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: volch5<\/p>\n<pre><code>max() (and min()) on DateTime objects compares them like dates (with timezone info) and returns DateTime object.\n<br>&lt;?php \n$dt1 = new DateTime('2014-05-07 18:53', new DateTimeZone('Europe\/Kiev'));\n$dt2 = new DateTime('2014-05-07 16:53', new DateTimeZone('UTC'));\necho max($dt1,$dt2)-&gt;format(DateTime::RFC3339) . PHP_EOL; \/\/ 2014-05-07T16:53:00+00:00\necho min($dt1,$dt2)-&gt;format(DateTime::RFC3339) . PHP_EOL; \/\/ 2014-05-07T18:53:00+03:00\n?&gt;<br>\nIt works at least 5.3.3-7+squeeze17<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: <\/p>\n<pre><code>Notice that whenever there is a Number in front of the String, it will be used for Comparison.\n<br>&lt;?php\n  max('7iuwmssuxue', 1); \/\/returns 7iuwmssuxu\n  max('-7suidha', -4); \/\/returns -4\n?&gt;<br>\nBut just if it is in front of the String\n<br>&lt;?php\n  max('sdihatewin7wduiw', 3); \/\/returns 3\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"min\">PHP <code>min()<\/code> Function<\/h2>\n<h3>PHP <code>min()<\/code> Usage<\/h3>\n<p>The PHP <code><code>min()<\/code><\/code> function will give you the lowest value in that array. If at least two parameters are provided, <code>min()<\/code> return the smallest of these values.<\/p>\n<h3>PHP <code>min()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> min ( array $values ) : mixed<\/code><\/pre>\n<pre><code class=\"lang-php\"> min ( mixed $value1 [, mixed $... ] ) : mixed<\/code><\/pre>\n<h3>PHP <code>min()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>values<\/code> \u2014 An array containing the values. <\/p>\n<\/li>\n<li>\n<p><code>value1<\/code> \u2014 Any comparable value. <\/p>\n<\/li>\n<li>\n<p><code>...<\/code> \u2014 Any comparable value. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>min()<\/code> Return Value<\/h3>\n<p>The PHP <code>min()<\/code> function returns the parameter value considered \"lowest\" according to standard comparisons. If multiple values of different types evaluate as equal (e.g. <strong>0<\/strong> and <strong>'abc'<\/strong>) the first provided to the function will be returned.<\/p>\n<h3>PHP <code>min()<\/code> Working Examples<\/h3>\n<h4>1. Example uses of min()<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho min(2, 3, 1, 6, 7);  \/\/ 1\necho min(array(2, 4, 5)); \/\/ 2\n\/\/ The string 'hello' when compared to an int is treated as 0\n\/\/ Since the two values are equal, the order they are provided determines the result\necho min(0, 'hello');     \/\/ 0\necho min('hello', 0);     \/\/ hello\n\/\/ Here we are comparing -1 &lt; 0, so -1 is the lowest value\necho min('hello', -1);    \/\/ -1\n\/\/ With multiple arrays of different lengths, min returns the shortest\n$val = min(array(2, 2, 2), array(1, 1, 1, 1)); \/\/ array(2, 2, 2)\n\/\/ Multiple arrays of the same length are compared from left to right\n\/\/ so in our example: 2 == 2, but 4 &lt; 5\n$val = min(array(2, 4, 8), array(2, 5, 1)); \/\/ array(2, 4, 8)\n\/\/ If both an array and non-array are given, the array is never returned\n\/\/ as comparisons treat arrays as greater than any other value\n$val = min('string', array(2, 5, 7), 42);   \/\/ string\n\/\/ If one argument is NULL or a boolean, it will be compared against\n\/\/ other values using the rules FALSE &lt; TRUE and NULL == FALSE regardless of the \n\/\/ other types involved\n\/\/ In the below examples, both -10 and 10 are treated as TRUE in the comparison\n$val = min(-10, FALSE, 10); \/\/ FALSE\n$val = min(-10, NULL, 10);  \/\/ NULL\n\/\/ 0, on the other hand, is treated as FALSE, so is \"lower than\" TRUE\n$val = min(0, TRUE); \/\/ 0\n?&gt;<\/code><\/pre>\n<h3>Important Points about PHP <code>min()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> Values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an integer as though it were <strong>0<\/strong>, but multiple non-numeric string values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied. <\/p>\n<\/li>\n<\/ol>\n<h2 id=\"round\">PHP <code>round()<\/code> Function<\/h2>\n<h3>PHP <code>round()<\/code> Usage<\/h3>\n<p>The PHP <code><code>round()<\/code><\/code> function will give you the rounded value of <code class=\"parameter\">val<\/code> to specified <code class=\"parameter\">precision<\/code> (number of digits after the decimal point). <code class=\"parameter\">precision<\/code> can also be negative or zero (default).<\/p>\n<h3>PHP <code>round()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) : float<\/code><\/pre>\n<h3>PHP <code>round()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>val<\/code> \u2014 The value to round. <\/p>\n<\/li>\n<li>\n<p><code>precision<\/code> \u2014 The optional number of decimal digits to round to. <\/p>\n<\/li>\n<li>\n<p><code>mode<\/code> \u2014 Use one of the following constants to specify the mode in which rounding occurs. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>round()<\/code> Return Value<\/h3>\n<p>The PHP <code>round()<\/code> function returns the value rounded to the given <code class=\"parameter\">precision<\/code> as a float.<\/p>\n<h3>PHP <code>round()<\/code> Working Examples<\/h3>\n<h4>1. round() examples<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\nvar_dump(round(3.4));\nvar_dump(round(3.5));\nvar_dump(round(3.6));\nvar_dump(round(3.6, 0));\nvar_dump(round(1.95583, 2));\nvar_dump(round(1241757, -3));\nvar_dump(round(5.045, 2));\nvar_dump(round(5.055, 2));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">float(3)\nfloat(4)\nfloat(4)\nfloat(4)\nfloat(1.96)\nfloat(1242000)\nfloat(5.05)\nfloat(5.06)<\/code><\/pre>\n<h4>2. How precision affects a float<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$number = 1346.21;\nvar_dump(round($number, 2));\nvar_dump(round($number, 1));\nvar_dump(round($number, 0));\nvar_dump(round($number, -1));\nvar_dump(round($number, -2));\nvar_dump(round($number, -3));\nvar_dump(round($number, -4));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">float(1346.21)\nfloat(1346.2)\nfloat(1346)\nfloat(1350)\nfloat(1300)\nfloat(1000)\nfloat(0)<\/code><\/pre>\n<h4>3. mode examples<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho 'Rounding modes with 9.5' . PHP_EOL;\nvar_dump(round(9.5, 0, PHP_ROUND_HALF_UP));\nvar_dump(round(9.5, 0, PHP_ROUND_HALF_DOWN));\nvar_dump(round(9.5, 0, PHP_ROUND_HALF_EVEN));\nvar_dump(round(9.5, 0, PHP_ROUND_HALF_ODD));\necho 'Rounding modes with 8.5' . PHP_EOL;\nvar_dump(round(8.5, 0, PHP_ROUND_HALF_UP));\nvar_dump(round(8.5, 0, PHP_ROUND_HALF_DOWN));\nvar_dump(round(8.5, 0, PHP_ROUND_HALF_EVEN));\nvar_dump(round(8.5, 0, PHP_ROUND_HALF_ODD));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">Rounding modes with 9.5\nfloat(10)\nfloat(9)\nfloat(10)\nfloat(9)\nRounding modes with 8.5\nfloat(9)\nfloat(8)\nfloat(8)\nfloat(9)<\/code><\/pre>\n<h4>4. mode with precision examples<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho 'Using PHP_ROUND_HALF_UP with 1 decimal digit precision' . PHP_EOL;\nvar_dump(round( 1.55, 1, PHP_ROUND_HALF_UP));\nvar_dump(round( 1.54, 1, PHP_ROUND_HALF_UP));\nvar_dump(round(-1.55, 1, PHP_ROUND_HALF_UP));\nvar_dump(round(-1.54, 1, PHP_ROUND_HALF_UP));\necho PHP_EOL;\necho 'Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision' . PHP_EOL;\nvar_dump(round( 1.55, 1, PHP_ROUND_HALF_DOWN));\nvar_dump(round( 1.54, 1, PHP_ROUND_HALF_DOWN));\nvar_dump(round(-1.55, 1, PHP_ROUND_HALF_DOWN));\nvar_dump(round(-1.54, 1, PHP_ROUND_HALF_DOWN));\necho PHP_EOL;\necho 'Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision' . PHP_EOL;\nvar_dump(round( 1.55, 1, PHP_ROUND_HALF_EVEN));\nvar_dump(round( 1.54, 1, PHP_ROUND_HALF_EVEN));\nvar_dump(round(-1.55, 1, PHP_ROUND_HALF_EVEN));\nvar_dump(round(-1.54, 1, PHP_ROUND_HALF_EVEN));\necho PHP_EOL;\necho 'Using PHP_ROUND_HALF_ODD with 1 decimal digit precision' . PHP_EOL;\nvar_dump(round( 1.55, 1, PHP_ROUND_HALF_ODD));\nvar_dump(round( 1.54, 1, PHP_ROUND_HALF_ODD));\nvar_dump(round(-1.55, 1, PHP_ROUND_HALF_ODD));\nvar_dump(round(-1.54, 1, PHP_ROUND_HALF_ODD));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">Using PHP_ROUND_HALF_UP with 1 decimal digit precision\nfloat(1.6)\nfloat(1.5)\nfloat(-1.6)\nfloat(-1.5)\nUsing PHP_ROUND_HALF_DOWN with 1 decimal digit precision\nfloat(1.5)\nfloat(1.5)\nfloat(-1.5)\nfloat(-1.5)\nUsing PHP_ROUND_HALF_EVEN with 1 decimal digit precision\nfloat(1.6)\nfloat(1.5)\nfloat(-1.6)\nfloat(-1.5)\nUsing PHP_ROUND_HALF_ODD with 1 decimal digit precision\nfloat(1.5)\nfloat(1.5)\nfloat(-1.5)\nfloat(-1.5)<\/code><\/pre>\n<h3>Changelog for PHP round()<\/code> Function<\/h3>\n<p><strong>5.3.0 \u2014 <\/strong> The <code class=\"parameter\">mode<\/code> parameter was introduced. <\/p>\n<p><strong>5.2.7 \u2014 <\/strong> The inner workings of <code>round()<\/code> was changed to conform to the C99 standard. <\/p>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: takingsides<\/p>\n<pre><code>In my opinion this function lacks two flags:\n- PHP_ROUND_UP - Always round up.\n- PHP_ROUND_DOWN - Always round down.\nIn accounting, it's often necessary to always round up, or down to a precision of thousandths.\n<br>&lt;?php\nfunction round_up($number, $precision = 2)\n{\n    $fig = (int) str_pad('1', $precision, '0');\n    return (ceil($number * $fig) \/ $fig);\n}\nfunction round_down($number, $precision = 2)\n{\n    $fig = (int) str_pad('1', $precision, '0');\n    return (floor($number * $fig) \/ $fig);\n}\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: depaula<\/p>\n<pre><code>As PHP doesn't have a a native number truncate function, this is my solution - a function that can be usefull if you need truncate instead round a number.\n<br>&lt;?php\n\/**\n * Truncate a float number, example: &lt;code&gt;truncate(-1.49999, 2); \/\/ returns -1.49\n * truncate(.49999, 3); \/\/ returns 0.499\n * &lt;\/code&gt;\n * @param float $val Float number to be truncate\n * @param int f Number of precision\n * @return float\n *\/\nfunction truncate($val, $f=\"0\")\n{\n    if(($p = strpos($val, '.')) !== false) {\n        $val = floatval(substr($val, 0, $p + 1 + $f));\n    }\n    return $val;\n}\n?&gt;<br>\nOriginally posted in http:\/\/stackoverflow.com\/a\/12710283\/1596489<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: slimusgm<\/p>\n<pre><code>If you have negative zero and you need return positive number simple add +0:\n$number = -2.38419e-07;\nvar_dump(round($number,1));\/\/float(-0)\nvar_dump(round($number,1) + 0);\/\/float(0)<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: djcox99<\/p>\n<pre><code>I discovered that under some conditions you can get rounding errors with round when converting the number to a string afterwards.\nTo fix this I swapped round() for number_format().\nUnfortunately i cant give an example (because the number cant be represented as a string !)\nessentially I had round(0.688888889,2);\nwhich would stay as 0.68888889 when printed as a string.\nBut using number_format it correctly became 0.69.<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: twan<\/p>\n<pre><code>If you'd only want to round for displaying variables (not for calculating on the rounded result) then you should use printf with the float: \n \n<br>&lt;?php printf (\"%6.2f\",3.39532); ?&gt;<br> \n \nThis returns: 3.40 .<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: Anonymous<\/p>\n<pre><code>Here is function that rounds to a specified increment, but always up. I had to use it for price adjustment that always went up to $5 increments. \n \n<br>&lt;?php  \nfunction roundUpTo($number, $increments) { \n    $increments = 1 \/ $increments; \n    return (ceil($number * $increments) \/ $increments); \n} \n?&gt;<br><\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Numerical Functions in PHP Function Name Function Description abs() Absolute value ceil() Round fractions up floor() Round fractions down fmod() Returns the floating point remainder (modulo) of the division of the arguments is_finite() Finds whether a value is a legal finite number is_infinite() Finds whether a value is infinite is_nan() Finds whether a value is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[8,5],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-reference","tag-math","tag-php"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}