Need to convert the floating values to integer format below the condition in php

63 views Asked by At

Need to convert the floating values to integer format below the condition

$val = 75.00 means the value must be show 75

$val = 75.50 means the values must be show 75.50

the floating points values .00 means no need to display otherwise will display with floating values.

is possible in php?

3

There are 3 answers

0
Waqas Shahid On

Try this code:

$val = 75.00;
$value = explode(".", $val);
$decimal_value = substr($value[0], 0, 1);

if($decimal_value == "00"){
   $val = $value[0];
} else {
   $val = number_format($val, 2, '.', '');
}
echo $val;
0
Lakshya On

$val + 0 does the trick.

echo 75.00 + 0; // 75
echo 75.50 + 0; // 75.5

Internally, this is equivalent to casting to float with (float)$val or floatval($val) but I find it simpler.

0
arulraj On

(float) $val;

In finally use below the code.

$ans = (float)$val;

echo $ans; means it working perfectly.