2

This is the function for formatting FLOAT number as INT if decimal part is 0.

$formatted = (int)$i + $i - (int)$i;

// $i = 15.12 , $formatted = 15.12
// $i = 15.00 , $formatted = 15

But it seems to be a bug in PHP. I hadn't found type cast for INT+FLOAT operation in PHP manual. In other language, like C++ INT+FLOAT = FLOAT.

1 Answer 1

1
<?php

$i = 15.00;

$formatted = (int)$i + $i - (int)$i;

var_dump($formatted);


float(15)

It's still a float.

The reason the 00 doesn't get shown is because it's exactly 15.

If you know the number is exactly 15, and you don't know the context where it came from, then you can't assume any precision.

For example, would you expect $f = (float) 15; to show 15.00? No, of course not. Same concept applies, just in the context it's in you expect 15.00.

Also, you may find number_format() or round() useful unless this was just an experiment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.