jovazel
jovazel

Reputation: 79

How to Convert/Round it off

I have some problem regarding converting/truncating a number.

This is the condition:

x.35 where x is any decimal number. If the decimal number is less than or equal to 35 then convert the .35 into 99 and subtract the x value with 1.

Something like this:

45.35 will become 44.99

Any help will be much more appreciated!

Upvotes: 0

Views: 51

Answers (2)

Michael
Michael

Reputation: 1886

Check out:
http://us.php.net/floor
http://us.php.net/ceil
http://us.php.net/round

Those should help

Following up on this, here's an example.

$val = 45.35;  
$decimal = $val - floor($val);
if($decimal<.35) echo floor($val).".99";

Upvotes: 0

Katie G.
Katie G.

Reputation: 46

There's probably a faster way that doesn't use the explode function and other unnecessary things, but here's my rendition of it.

    <?php
    $input = 45.35; //input, obviously
    $in2 = explode(".", $input);

    if($in2[1] <= 35) {
    $in2[1] = 99;
    }

    $output = $in2[0] . "." . $in2[1];
    ?>

Upvotes: 1

Related Questions