This question already has answers here:
int((0.1+0.7)*10) = 7 in several languages. How to prevent this?
(7 answers)
Closed 8 years ago.
Today i started writing a small PHP code and it left me confusing and so i parked here.
<?php
echo (int) ((0.5 + 0.3) * 10); // Outputs 8 as expected
<?php
echo (int) ((0.1 + 0.7) * 10); // Outputs 7 . How ????
Can someone answer with a detailed explanation ?
It is because floating point representation in computers is not exact for some numbers. As already said in the comments, 0.7 is represented internally as 0.699999 or so.
There are two websites that continuously pop up in these kind of questions:
http://floating-point-gui.de/
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
I prefer the first one, as it is a little lighter on the academics. Read that information and you'll understand.
Related
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 3 years ago.
I'm trying to show one decimal place for rating number I have
but it returns unexpected value.
I used number_format and the round functions and both have the same issue, or i'm doing something wrong.
I tried to make this number show one decimal number
4.96 and it always returns 5 instead of 4.9
number_format(4.96, 1)
round(4.96, 1)
round(4.96, 1,PHP_ROUND_HALF_DOWN)
both functions returns 5 instead of 4.9
I searched all answers but couldn't find anything helpful.
Rounding 4.96 will round .9 up, so it will be 5 in all cases. If you want to do it without rounding, you may have to tweak it a bit to fool it:
floor(4.96 * 10) / 10; // 4.9
Here's a function you can use to achieve this.
function convertToSingleDecimal($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
print convertToSingleDecimal("4.96", 1);
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What does the percent sign mean in PHP?
(6 answers)
Closed 3 years ago.
I just started to learn PHP deeper for Zend PHP certification and I found this code, which actually works. Can someone explain me the logic behind this?
<?php
$num = 20% - 8;
echo $num; // 4
What you're seeing is the modulus operator, which in essence asks "What is the remainder of 20 divided by -8".
So you might ask, why isn't it negative 4? From the manual,
The result of the modulo operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.
See the PHP: Arithmetic Operators for more documentation.
% is not per cent, but modulus, cf. https://en.m.wikipedia.org/wiki/Modular_arithmetic
20 = 2 * 8 + 4, therefore, 20 % 8 = 4
This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 4 years ago.
I'm probably not looking hard enough but the common question about php rounding is rounding up, not down.
For example I am trying round this..
<?php $roundDown = 768; ?>
Down to..
<?php var_dump($roundDown) /* 700 */ ?>
Whats the simplest method to do this, or is it because the number is closer to 800 that it's not technically rounding?
Whats the function that I need to do this if it's not rounding?
A little point in the right direction would be much appreciated.
You can try ceil() and floor() function.
echo floor(768 / 100) * 100; // Output:700
echo ceil(768 / 100) * 100; // Output:800
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm issuing a strange behavior with intval in PHP 7.0
It seems like using intval after a floating number computation returns wrong values.
Here's an example:
echo intval(920); // This prints 920 as expected
echo intval(9.2 * 100); // this prints 919!!!
Probably I'm misunderstanding the correct usage of intval.
Can someone explain me why this happens?
Can you please try this:
(int)(9.2 * 10)
OR
See the example below, this is from a comment in the documentation
$n="19.99";
print intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In PHP, how to print a number with 2 decimals, but only if there are decimals already?
Brothers I want to convert me any way I entered into a text field automatically turns
12 to 12.00
How possible work by php and javascript
Thanks
In PHP its easy to do that by doing:
number_format($numberVar, 2);
Try this number_format() from PHP.net:
echo number_format(12, 2)
What, no javascript?
var x = 3;
alert(x.toFixed(2)); // 3.00
Will "work" for the case given, but so will:
alert(x + '.00');
There are bugs in toFixed in some older browsers with certain values so many write their own simple routine.
$thisnum = $thisnum . ".00";
Proof of concept:
$thisnum = $thisnum . ".00";
echo ($thisnum + 4.55);
However, as pointed out by the community, this is something that will only work for the case mentioned in the question and will not handle any use cases that already have decimals. The other answers are better.