0

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?

arulraj
  • 105
  • 2
  • 12
  • 3
    Possible duplicate of [Floating point number format](http://stackoverflow.com/questions/1734233/floating-point-number-format) – Dinidu Hewage Jan 14 '16 at 05:05

3 Answers3

0

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;
Waqas Shahid
  • 1,051
  • 1
  • 7
  • 22
0

$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.

Lakshya
  • 506
  • 1
  • 4
  • 20
0

(float) $val;

In finally use below the code.

$ans = (float)$val;

echo $ans; means it working perfectly.

arulraj
  • 105
  • 2
  • 12