Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number format with a variable decimal place

Tags:

php

Is there a built-in/neat way to format a number (just like number_format does), but without any rounding ups/downs?

For instance, number 1234.234 should be formatted as 1,234.234 and another number 1234 should be formatted as 1,234 (i.e. without any trailing .000)

like image 220
AVK Avatar asked Apr 30 '26 02:04

AVK


2 Answers

You can define simple custom function for that:

<?php
    function custom_number_format($number, $decimal = '.')
    {
        $broken_number = explode($decimal, $number);
        if (isset($broken_number[1]))
            return number_format($broken_number[0]) . $decimal . $broken_number[1];
        else
            return number_format($broken_number[0]);
    }

    $n1 = '1234.234';
    $n2 = '1234';

    echo custom_number_format($n1);
    echo '<br>';
    echo custom_number_format($n2);
?>

Output is:

1,234.234
1,234
like image 142
mitkosoft Avatar answered May 01 '26 16:05

mitkosoft


Based on the arhey's answer

TLDR ;)

You can use number_format to format the number to a fixed-width format, then use rtrim twice to remove trailing zeroes, and dot.

rtrim(rtrim(number_format($number, 3, '.', ','), '0'), '.')

Starting from the last character, rtrim removes it while it is one of those given. In our case, we remove trailing dots, then we remove an eventual trailing zero.

rtrim(rtrim(number_format(1234.123, 3, '.', ','), '0'), '.')
// returns 1,234.123

rtrim(rtrim(number_format(1234.12, 3, '.', ','), '0'), '.')
// returns 1,234.12 (1,234.120, trimmed to 1234.12)

rtrim(rtrim(number_format(1234, 3, '.', ','), '0'), '.')
// returns 1,234 (1,234.000, trimmed to 1234)

rtrim(rtrim(number_format(1200, 3, '.', ','), '0'),'.')
// returns 1,200 (1,200.000, trimmed to 1200., trimmed to 1200)

Formal form, and discussion about the parameters (notably the decimals count)

rtrim(rtrim(number_format($number, <N>, '<D>', ''), '0'), '<D>')

Where :

  • D is the decimal separator. To avoid locale-formatting problems, explicitly specify it
  • N is the maximum digits you number can have.

If you know all your numbers will have less than 3 digits, go and take N=3.

What if you don't know how many decimals are at most ? Well, things are getting more complex. It may worth recalling (as stated in the PHP documentation) that floats are stored :

  • with a precision (a number of digits, without distinction whether they are before or after the decimal separator), not a number of decimals
  • and in their binary form, not their decimal one, and that can lead to rounding errors when reaching precision limit.

For example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So there is no universal good value, you'll have to choose it depending on the usual scale of your data.

And that explains why there is no built-in function for that : PHP can't choose for you.

like image 37
4 revsPierre-Olivier Vares Avatar answered May 01 '26 14:05

4 revsPierre-Olivier Vares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!