This question already has an answer here:
PHP - Get rid of notation
(1 answer)
Closed 8 years ago.
I have
$a = $rowcount + $rowcount2 ; // this is 25
$b = 1000000000;
$total = $a / $b;
this gives me $total = 2.5E-8; but I want 0.000000025. What is causing this?
You need to tell PHP how to format your number
printf('%.9f', $a / $b);
or to store in a variable...
$num = sprintf('%.9f', $a / $b);
Related
This question already has answers here:
Is there a PHP function for swapping the values of two variables?
(20 answers)
Swap two variables value without using third variable in php [duplicate]
(2 answers)
Closed 4 years ago.
In this code
$x = 1;
$y = 2;
echo $x.$y; //12
I want to replace $x and $y vlaues with each other, So $x = 2 and $y = 1, Is that possible to be achieved without assigning middle-variables? Something like using a ready-function or like
$x = 1;
$y = 2;
echo $x.$y; //12
$x = &$y;
$y = &$x;
echo $x.$y //22
But instead of 22 I want to get 21.
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
<?php
var $a = 85;
var $b = 7;
var $c = $a/$b;
echo $c;
?>
Answer is 12.14285714285714
I want to fixed it as 12.14. How can we set two digits after decimal in PHP?
PHP doc : PHP Doc to round numbers
In your example :
var $b = 7;
var $c = $a/$b;
echo round($c, 2);
Hope it helps
You can use number_format:
number_format($c, 2)
This question already has answers here:
Compare floats in php
(17 answers)
Closed 6 years ago.
I'm struggling to make accounting software in PHP. And i got some bug from float precision.
Here is the sample unworking code :
$a = (float) 2258574.18;
$b = (float) 2058555.18;
$c = 200019;
$d = 0;
($b+$c-$d == $a ) ? $x = "equals" : $x = "!equals";
echo $x;
Outputs :
!equals
I was using round() abs() but doesnt solved.
It work if i only convert it to int (int).
I almost suicide because of this.
Try round() with precision 2
$e = $b+$c-$d;
(round($e, 2) == round($a, 2) ) ? $x = "equals" : $x = "!equals";
echo $x;
This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
without use of round() function perfrom the round() in php
$a = "123.45785";
$v = round($a);
output: 123.46;
it had done by round function but i want to get output without use of round and number_format() function.
Here's a way of doing it with arithmetics:
function my_round($num, $places = 2) {
// Multiply to "move" decimals to the integer part
// (Save one extra digit for rounding)
$num *= pow(10, $places + 1);
// Truncate to remove decimal part
$num = (int) $num;
// Do rounding based on the last digit
$lastDigit = $num % 10;
if ($lastDigit >= 5)
$num += 10;
// Remove last digit
$num = (int) ($num/10);
// "Move" decimals in place, and you're done
$num /= pow(10, $places);
return $num;
}
You have sprintf.
$a = "123.45785";
echo sprintf("%01.2f", $a); // output: 123.46