I would like to use the number_format()
function without to specify the number of decimals. (If 2 decimals, display 2, if 5, display 5)
Is that possible?
Thanks a lot
I would like to use the number_format()
function without to specify the number of decimals. (If 2 decimals, display 2, if 5, display 5)
Is that possible?
Thanks a lot
If this is really what you want, this may work even if this is totally useless
function numberOfDecimals($value)
{
if ((int)$value == $value)
{
return 0;
}
else if (! is_numeric($value))
{
// throw new Exception('numberOfDecimals: ' . $value . ' is not a number!');
return false;
}
return strlen($value) - strrpos($value, '.') - 1;
}
$number = 1.23456;
print number_format($number,numberOfDecimals($number));
credits : PHP: get number of decimal digits
You can calculate the number of decimals before you use numberformat and use that in the function.
$number = 50001/4;
If(strpos($number, ".") !== false){
// Is float
$decimals = strlen($number) - (strpos($number, ".")+1);
}Else{
// Is integer == no decimals
$decimals =0;
}
Echo number_format($number, $decimals, ',', ' ');