Invalid value when using floats [duplicate] - php

This question already has answers here:
PHP - Floating Number Precision [duplicate]
(8 answers)
Closed 9 years ago.
The following operation give out the wrong result.
$First = '45.4000';
$Second = '4.6800000000';
$Third = '50.00';
echo ( $First + $Second ) - $Third;
OUTPUT: 0.079999999999998
Expected Output: 0.08
I am looking on how to get the right result, without using number_format/sprintf ...etc.
As this issue is affecting multiple places in my code & have to go over everything & formatting it is a pain.

As a "quick fix", change the precision setting in your php.ini file. Documentation.
By default, it is 14, which is more than you need almost all the time (and if you need that much precision you'd be using a dedicated math library). Change it to something like 4, and the result will be rounded to that length - note that you can still override this with number_format on a case-by-case basis if you need to.

Try This
$First = '45.4000';
$Second = '4.6800000000';
$Third = '50.00';
$sk = ( $First + $Second ) - $Third;
echo round($sk,4);
?>

Related

Why show incorrect result with php function? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why show incorrect result with php function ?
This is my code. It's must to show 0
But when i test this code. It's show 99
Why ?????
<?PHP
$first = "8.12";
$second = "0.12";
$first = ($first)*(100);
$second = ($second)*(100);
$results = ($first)-($second);
echo ($results)%(100);
?>
This seems to be a weird issue with floating point variables (double). Here's how you can fix it without worrying about casting variables:
$first = "8.12";
$second = "0.12";
$first = ($first)*(100);
$second = ($second)*(100);
$results = round(($first)-($second));
echo ($results)%(100);
Adding round to the results calculation (or, if you want, when multiplying $first and $second with 100) will solve the floating point error.
You can also specify the precision of round() with a second argument, such as
round(123.4567, 2); // 123.46

Need one digit after decimal without changing any number [duplicate]

This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);

How to print rounded percent [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
hello i'm trying to find %. first i found the seconds
$tm=sum_the_time($d_duration);
$d_seconds='0';
list($hour,$minute,$second) = explode(':', $tm);
$d_seconds += $hour*3600;
$d_seconds += $minute*60;
$d_seconds += $second;
$total_second=$c_seconds+$p_seconds+$t_seconds+$b_seconds+$d_seconds;
$c_seconds=$c_seconds*100/$total_second;
$p_seconds=$p_seconds*100/$total_second;
$t_seconds=$t_seconds*100/$total_second;
$b_seconds=$b_seconds*100/$total_second;
$d_seconds=$d_seconds*100/$total_second;
echo $c_seconds;
the result is 10.754504504505, how would I print this code like 10.7
You can try using printf() function:
printf("%.1f", $c_seconds);
Or, number_format():
echo number_format( $c_seconds, 1 );
These two functions will round your number (will return 10.8 in your example), so, if you want to just truncate to the first decimal place (result to be equal to 10.7), you can use the following:
echo substr($c_seconds, 0, strpos($c_seconds, ".") + 2);
Actually, you can use the solutions from this question to better use number_format() and get your desired result.
echo sprintf('%0.1f', $c_seconds);
relevant docs here: http://php.net/sprintf
http://php.net/manual/en/function.number-format.php numberformat is propably what you are looking for.

PHP number format drops zero when rounding [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
I'm using PHP's number_format to display floats to 2 decimal places.
When the number is something like 1.898 it gets rounded up to 1.9.
How do I get it to display that as 1.90?
Update:
I have a function that ends...
return number_formant($num, 2);
The php script that calls the function prints out the number to be used by Javascript. When I do a var_dump on the number, it prints correctly with two decimal places. Looks like it's Javascript that's loosing the zero.
Here's the JS code that was causing the issue...
function show_level(level) {
...
if (level > 9999)
level_label = (level / 1000).toPrecision(3) + 'k';
else if (level > 999)
level_label = (level / 1000).toPrecision(2) + 'k';
else
level_label = level;
I altered the last line to get it working how I wanted..
level_label = level.toFixed(2);
Maybe not the best solution but:
$n = 1.2345;
$n = number_format( round($n, 1), 2);
//echo 1.20
echo $n;
So,
you mean something like this (what you have):
<?php
echo number_format($x);
?>
and here is what you want:
<?php
echo number_format($x,2);
?>
you can use other lengths. Try it Out!
Hope could helpya
:)
echo sprintf('%01.2f', number_format(1.898,2))
You can do:
echo round(1.898,2);

Fixing to .2 decimal number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Default Number of Decimal Places to Output in PHP
basically a bit of a maths problem,
$average_ppm = $total_points_given / $totalvalue;
$average_ppm now equals 2.432608695652174, I don't want to display these numbers, I just need $average_ppm to be 2.43, so to a fixed 2 decimal points. How can I do this??
Thanks for anyones time.
Use sprintf if you want a string output, or round/floor/ceil for a numeric value:
$average_ppm = 2.432608695652174;
echo sprintf("%.2f", $average_ppm); // 2.43
$approx_average_ppm = round($average_ppm, 2);
echo $approx_average_ppm; // 2.43
echo floor($average_ppm, 2); // 2.43 , even if $average_ppm = 2.439
echo ceil($average_ppm, 2); // 2.44
You could either use sprintf, round or floor/ceil depending on how you want the numbers rounded.
Most suited for your need would be round:
$average_ppm = round($total_points_given / $totalvalue,2);
If you want to have ALWAYS 2 numbers after... you can do it with number_format:
number_format(2.43260869565217, 2); // 2.43
When you got a number like: 2.400054846 and you use round you will get 2.4
and if you want it with 2 number behind you can use number_format this will output 2.40
I would use Round:
round($average_ppm, 2);
You could use the bcmath functions if they are available where the third argument is the precision
$average_ppm = bcdiv($total_points_given, $totalvalue, 2);

Categories