How to echo number format like this using php ?
0.50 echo to 0.5
1.00 echo to 1
1.50 echo to 1.5
1.95 echo to 1.95
I tried to use number_format
, ceil
, floor
, round
But not work.
How can i do that ?
Thank you for help.
How to echo number format like this using php ?
0.50 echo to 0.5
1.00 echo to 1
1.50 echo to 1.5
1.95 echo to 1.95
I tried to use number_format
, ceil
, floor
, round
But not work.
How can i do that ?
Thank you for help.
It's really silly, but you can simply add 0
to get this effect.
echo "0.50" + 0;
echo "1.00" + 0;
This can do a trick:
<?php
$string = "1.50";
echo (float)$string;
?>
This gives
1.5
try this function
<?php
$data = "1.00";
$arr = explode(".",$data);
$formated_nunber=formatNumber($arr);
function formatNumber($arr){
if($arr[1]==00){
$finalStr=$arr[0];
return $finalStr;
}
else if(substr($arr[1],-1)==0){
$arr[1]=rtrim($arr[1],substr($arr[1],-1));
$finalStr=implode(".",$arr);
return $finalStr;
}else{
$finalStr=implode(".",$arr);
return $finalStr;
}
}
echo "<pre>";print_r($formated_nunber);
//echo $whatIWant;
?>
Use number_format(0.50, 1)
. That will do the trick.