6

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:

  1. Whether a given number is decimal i.e having "." and digits after decimal
  2. Whether the last digit is 0, if yes, then get rid of the last digit
  3. Whether both the digits after decimal points are 0, if yes, remove them.
  4. If any of them are not 0, then they should remain there, like in case of 99.09 it should remain intact, in case of 99.90 it should be 99.9.

Awaiting your ideas. Thanks in advance.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Asnexplore
  • 363
  • 1
  • 7
  • 25

4 Answers4

3

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.

user2342558
  • 5,567
  • 5
  • 33
  • 54
2

you should be able to just wrap it in a floatval()

0

Try this:

echo round(99.001, 2), PHP_EOL;
echo round(99.901, 2), PHP_EOL;
echo round(99.011, 2), PHP_EOL;

Output:

99
99.9
99.01
dhinchliff
  • 1,106
  • 8
  • 17
  • 1
    `number_format` rounds as well so I think Asnexplore was ok with it rounding. Your answer is the cleanest solution (all he needs is that, nothing more), no idea why the downvote. – IncredibleHat Jan 26 '18 at 15:41
  • 1
    @IncredibleHat You are right, he does do a rounding already so I removed the extra stuff I added. – dhinchliff Jan 26 '18 at 15:46
  • Yes, true the rounding etc. was all done. I need these results post that to cleanup the trailing 0 or 0s. – Asnexplore Jan 27 '18 at 04:19
  • Try with numbers like 1200.91, 1,200.00, 1,200.90, 1,200. They are showing as 1 if the comma is not there, but with commas they are generating an error message Parse error: syntax error, unexpected ',' in /tmp/execpad-edf220d84a3a/source-edf220d84a3a on line 2 – Asnexplore Jan 30 '18 at 09:36
  • Though I appreciate your hard work in helping me, unfortunately, it's still not doing what I wanted to have. – Asnexplore Jan 30 '18 at 09:37
  • That's because everything after the comma is ignored. Don't use commas, numbers don't have commas, you are using strings. The input should be number `1200.90` and not the string `1,200.90`. – dhinchliff Jan 30 '18 at 10:02
0

If you use a custom notation with number_format, you can use preg_replace to remove the decimal point separator and trailing zeroes after it.

// Default notation
echo preg_replace('/\.?0+$/', '', number_format(9990.00, 2)); // "9,990"
echo preg_replace('/\.?0+$/', '', number_format(9990.90, 2)); // "9,990.9"
echo preg_replace('/\.?0+$/', '', number_format(9990.01, 2)); // "9,990.01"

// French notation
echo preg_replace('/,?0+$/', '', number_format(9990.00, 2, ',', ' ')); // "9 990"
echo preg_replace('/,?0+$/', '', number_format(9990.90, 2, ',', ' ')); // "9 990,9"
echo preg_replace('/,?0+$/', '', number_format(9990.01, 2, ',', ' ')); // "9 990,01"
Matias Kinnunen
  • 7,828
  • 3
  • 35
  • 46