I am having few values 99.00 99.90 99.01
FYI, I am already getting the above 3 values after having number_format($value, 2) applied to them
But now I want to strip the decimals without rounding them off such as 99.00 to 99 99.90 to 99.9 99.01 to 99.01 remains same
How can achieve this? Please devise me a way guys.
I just need a function which checks following:
Awaiting your ideas. Thanks in advance.
By adding 0
to the number, the rightmost zeros are removed:
$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;
$num1 = number_format(99.00, 2, '.', '') + 0;
$num2 = number_format(99.90, 2, '.', '') + 0;
$num3 = number_format(99.01, 2, '.', '') + 0;
$num4 = number_format(99.012, 2, '.', '') + 0;
$num5 = number_format(99.0123, 2, '.', '') + 0;
echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";
Output:
99
99.9
99.01
99.01
99.01
Try it here.
With a function
:
function round2no0(&$num)
{
$num = number_format($num, 2, '.', '') + 0;
}
usage:
$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;
round2no0($num1);
round2no0($num2);
round2no0($num3);
round2no0($num4);
round2no0($num5);
echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";
function round2no0(&$num)
{
$num = number_format($num, 2, '.', '') + 0;
}
Output:
99
99.9
99.01
99.01
99.01
Edit:
Added , '.', ''
parameters to number_format
to handle also numbers with thousands maintaining the machine-format 12345.12
.
Try it here.
you should be able to just wrap it in a floatval()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With