This question already has answers here:
Truncate float numbers with PHP
(14 answers)
Closed 9 years ago.
I have a float number in PHP : 0.966666666667
I would like to print it like : 0.96 I used round() and number_format() but they give me both
0.97 is there a function to do that please ?
You can do this:
$num = 0.966666;
$num = floor($num * 100) / 100;
The best way to do this I've found is this:
//$val - the value to truncate
//$dist - the number of digits after to decimal place to keep
function truncate($val, $dist) {
//get position of digit $dist places after decimal point
$pos = strpos($val,'.');
if($pos !== false) {//if $val is actually a float
//get the substring starting at the beginning
//and ending with the point $dist after the
//decimal, inclusive -- convert to float.
$val = floatval(substr($val, 0, $pos + 1 + $dist));
}
return $val;
}
Then just call truncate($YOUR_NUM, 2);.
Source: https://stackoverflow.com/a/12710283/3281590
Related
This question already has answers here:
Convert a string containing a number in scientific notation to a double in PHP
(8 answers)
Closed 1 year ago.
I want to trim my decimal into something like 8.063 instead of the original which is 8.0638304611694E-9. I have implemented a function for it but it doesn't work when there is E-9 in it. Which part should I modify??
public function setPrecision($number, $decimals = 0)
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = 10 ** $decimals;
return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}
EDIT
The current implementation gave me 0 when I try to call the function.
setPrecision(8.0638304611694E-9, 3); // 0
In PHP, there are (at the moment of writing) 8519 builtin functions. One of them probably does the trick!
You could use log10() and round() in your function:
function setPrecision($number, $precision = 0)
{
$exponent = floor(log10($number)) - 1;
return round($number, -$exponent + $precision - 1);
}
Relative rounding with a certain number of digits can easily be done with sprintf.
$round = (float)sprintf('%0.3E', 8.0638304611694E-9);
var_dump($round); //float(8.064E-9)
On this basis I have this function which rounds float values with a certain relative decimal precision.
/*
* #return Float-Value with reduced precision
* #param $floatValue: input (float)
* #param $overallPrecision: 1..20 (default 10)
*/
function roundPrecision($floatValue, $overallPrecision = 10)
{
$p = min(20,max(0,$overallPrecision-1));
$f =(float)sprintf('%.'.$p.'e',$floatValue);
return $f;
}
example 1
$float = 0.0000123456789;
$newFloat = roundPrecision($float,5);
printf('%0.10f',$newFloat); //0.0000123460
example 2
$float = 3456.7891234;
$newFloat = roundPrecision($float,5);
printf('%0.10f',$newFloat); //3456.8000000000
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
<?php
$result = array_filter($_POST);
if(!empty($result)){
echo ((max($result)+min($result))/2)*1.2 ."|".((max($result)+min($result))/2)*1.3;
}
?>
hi all..how to set min and max result to 2 decimals places for the code above? actually the result will appears in input type text after the process complete
Use number_format function in PHP.
number_format ( float $number [, int $decimals = 0 ] ) : string
For more information see here
$yourNumber = 1235.343;
echo number_format ($yourNumber, 2);
// Will output 1,235.34 (with two decimals, specified in number_format as second paramter.
Edit: Max / Min functions return mixed value. Make sure its float and then pass it to number_format. It will returns you the string.
This question already has answers here:
PHP How do I round down to two decimal places? [duplicate]
(16 answers)
Closed 4 years ago.
Is there any way to do a regex that cuts off a number at a certain point without rounding (simply drops the digits?) say after 4 digits.... It will not be handling negative numbers, EVER. I could have number inputs such as 0.03123 or 1.31, or 10000.98, etc .... What I have written so far as my solution is rounding and not what I'm seeking....
$number = 10000.51999999;
$precision = 4;
echo "<br>";
// grab number before decimal by rounding down the whole number down...
$numberBeforeDecimal = floor($number);
echo "<br>";
// grab the decimal and set the correct precision needed
$n = $number;
intval($n); // 12
$theDecimalPart = explode('.', number_format($n, ($precision)))[1]; // 3430
echo $theDecimalPart; // this is outputting 5200
$theNewValue = $numberBeforeDecimal.".".$theDecimalPart;
explode() the number to get integer and decimal part separated out in an array
Use substr() function to get relevant precision from the decimal part.
Finally, concatenate them back.
Try the following (Rextester DEMO):
$number = 10000.51999999;
$precision = 4;
// separate out the integer and decimal part
$number_str_arr = explode('.', $number);
// concatenate them back
$theNewValue = $number_str_arr[0] . '.' . substr($number_str_arr[1], 0, $precision);
echo $theNewValue; // displays 10000.5199
This question already has answers here:
How to remove the leading character from a string?
(10 answers)
Closed 5 years ago.
What is the best way to remove zero before decimal point in php?
These are the two I know of:
ltrim(0.357, '0'); //.357
preg_replace("/0\./i", ".", 0.357); //.357
Are there any better method? Which of them is faster?
You can do it mathematically with floor() function
$n = 0.375;
$whole = floor($n); // 0
$fraction = $n - $whole; // .375
This question already has answers here:
Zero-pad digits in string
(5 answers)
Number with 0 on the front? [closed]
(2 answers)
Closed 8 years ago.
I am trying to extract part of a number using sustr() but the following is not working:
$num = 012014;
echo substr($num, 0,2);
returns 51
BUT
$num = '012014';
echo substr($num, 0,2);
returns 01
I want it to return 01 can someone help me
Is is not a normal number, when it's prepended with a zero (0). Then it's an octal number
If you treat $num as a string, it'll work.
$num = '012014';
echo substr($num, 0,2);
I guess you must declare the variable as string like this.
$num = '012014';
echo substr( (string)$num, 0, 2 );
Ooops I didn't notice the leading zero. You should just define $num as a string
$num = '0123';
Please try this
$num = 123424;
$num = (string)$num;
echo substr($num, 0,2);