3

Possible Duplicate:
PHP: show a number to 2 decimal places

How do I cast an integer, let's say $i=50 to a float with two decimal values: $m=50.00? I have tried (float)$i but nothing happens.

EDIT:

I need to make $i == $m so that returns TRUE;

Community
  • 1
  • 1
menislici
  • 1,149
  • 5
  • 18
  • 35
  • http://php.net/manual/en/function.number-format.php – PeeHaa Oct 20 '12 at 23:49
  • http://stackoverflow.com/questions/1604696/php-float-calculation-2-decimal-point – PeeHaa Oct 20 '12 at 23:51
  • Another users that changes his entire question after answers were posted, making old answers useless, even wrong. For the question. `$i==$m` returns `true`, so please provide your real code. Are you sure you are not comparing a `string` to a `number`? – clentfort Oct 21 '12 at 00:09

3 Answers3

9

round((float)$i, 2) Should do the trick.

The round function is built in and rounds a given float to the number of decimal places specified in the given argument.

Ahh yes, number_format($var, 2) is good as well !

dazed-and-confused
  • 1,293
  • 2
  • 11
  • 19
davepmiller
  • 2,620
  • 3
  • 33
  • 61
8

If you're just using the regular equality operator (==) instead of the type-safe equality operator (===) you shouldn't have any problems.

Comparing a double to an int with the same values:

$i = 20;
$m = 20.00;

gettype($i); // integer
gettype($m); // double

$i == $m; // true;
$i === $m; // false, $m is a double and $i is an integer.

If we would like to fix that, however, we just need to do:

$i = (double)$i;
gettype($i); // double
$i === $m; // true!
nyson
  • 1,055
  • 6
  • 20
  • 1
    +1 on the type .. but still would not return `50.00` as float .... see http://codepad.viper-7.com/CCAoaW ... – Baba Oct 21 '12 at 00:17
  • 2
    Why the interest in how `var_dump()` prints? It's only debug messaging, it **should** print the value in the most simple way. The OP was interested in how to make a floating point number (float and double are the same in php ([explained](http://stackoverflow.com/questions/3280892/difference-between-float-and-double-in-php))) be equal to an integer. `(float)$i` and `(double)$i` both return floats in `var_dump()` and double in `gettype()`. – nyson Oct 21 '12 at 00:24
  • The original question was how to print an `float` that has no decimal part so it shows zeroes there. Then the authored changed the question. And this Baba guy does not understand what datatypes are and how they work in PHP. – clentfort Oct 21 '12 at 00:38
  • Then casting isn't really related to the the question. If I'm not wrong, neither number_format() or sprintf() cares about the type of the arguments, as long as it's a number. – nyson Oct 21 '12 at 00:45
7

For floating point numbers the number of decimal digits is a formatting property, that is the number itself doesn't know about those things. A float 50 is stored as an exact integer 50. You need to format the number (using sprintf for example) to be written with decimal digits.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • if `$i=50` is stored as `$m=50.00` than `$i==$m` would return `TRUE`. However, that's not the case in my application, it returns `FALSE`. – menislici Oct 20 '12 at 23:53
  • It's not. But floating-point numbers that contain integers can represent those integers exactly. Read up on how they look in memory, you'll notice the same. *Decimal* numbers, like .NET supports, for example, store their precision as well, which is why you can have a decimal number with 2 digits of precision. But not a normal `float`. – Joey Oct 20 '12 at 23:57
  • `$i==$m` returns `true`. `$i===$m` returns `false` since the `===` operator also compares the datatypes which are `int` and `float` in your case, while the `==` will automatically convert the `int` to a `float` for the comparison. The actual variable will not be effected. – clentfort Oct 21 '12 at 00:01
  • `Uhm, of course it's a valid floating-point number` ??? tell me how php would show `50.00` as float ... – Baba Oct 21 '12 at 00:02
  • @Joey Next time do your research more on PHP .. there is no way to get `50.00` as float all you get is `50` in php. You can only get it has a string – Baba Oct 21 '12 at 00:20
  • @Baba Joey did his research. Read his answer more carefully. http://en.wikipedia.org/wiki/Floating_point The number 50.00 and 50 are represented as `5 * 10^1`. – Bailey Parker Oct 21 '12 at 00:30
  • @PhpMyCoder yes ..... i know what wikipedia says.. but in PHP there is no way to represent it that way ... even if you have `$a = 50.00` PHP auto converts it to `50` ... It would be nice for you to run some test too .. tested in `10 different php version` http://sandbox.onlinephpfunctions.com/code/301e41babd89ab3ae267ae0e21b1a659353fe6a2 .. you would be surprised .. ans that what have been saying – Baba Oct 21 '12 at 00:34
  • @Baba PHP doesn't "auto convert" anything. http://codepad.viper-7.com/eAGKYJ When you `var_dump(50.00)` it echoes `float(50)`. The number isn't converted; it is still a float. But the computer represents 50.00 in it's memory as `50 * 10^1`. This has nothing to do with PHP, because the same thing happens in C: http://codepad.org/VxR89E1d – Bailey Parker Oct 21 '12 at 00:40
  • @PhpMyCoder You forgot the `return 0` in your C code. ;) Also stop trying to argue with that Baba guy, read his answer. I tried to explain him the difference between a datatype and how things are printed to the user, he does not want to understand. Also he doesn't know the difference between an int and a float. This is fighting windmills. – clentfort Oct 21 '12 at 00:48
  • @clentfort all i asked is show me `50.00` as float in PHP and you said ` learn what === actually does, what var_dump does` ??? `===` in PHP is used to compare types [TRUE if $a is equal to $b, and they are of the same type.](http://php.net/manual/en/language.operators.comparison.php) .. am not fight with anyone here .. just want to lean ... at least to me a test case that proves otherwise ... no body has done that – Baba Oct 21 '12 at 00:51
  • @Baba: Join the chat I suggest you in your answer, I try to explain – clentfort Oct 21 '12 at 00:55
  • @PhpMyCoder thanks for the reply. `C`types and PHP are a bit diffident. PHP still says `50.00 is not 50` ... Do you have any way to prove this without using `==` because it returns `TRUE if $a is equal to $b after type juggling.` only way to validated variables with their type in PHP is `===` am not sure of any other way – Baba Oct 21 '12 at 00:57
  • @clentfort where is the link to the chart – Baba Oct 21 '12 at 00:57
  • http://chat.stackoverflow.com/rooms/18352/discussion-between-clentfort-and-baba – clentfort Oct 21 '12 at 00:58