Remove useless zero digits from decimals in PHP

PhpNumbersDecimal

Php Problem Overview


I'm trying to find a fast way to remove zero decimals from number values like this:

echo cleanNumber('125.00');
// 125

echo cleanNumber('966.70');
// 966.7

echo cleanNumber(844.011);
// 844.011

Does exists some optimized way to do that?

Php Solutions


Solution 1 - Php

$num + 0 does the trick.

echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7

Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.

Solution 2 - Php

you could just use the floatval function

echo floatval('125.00');
// 125

echo floatval('966.70');
// 966.7

echo floatval('844.011');
// 844.011

Solution 3 - Php

This is what I use:

function TrimTrailingZeroes($nbr) {
	return strpos($nbr,'.')!==false ? rtrim(rtrim($nbr,'0'),'.') : $nbr;
}

N.B. This assumes . is the decimal separator. It has the advantage that it will work on arbitrarily large (or small) numbers since there is no float cast. It also won't turn numbers into scientific notation (e.g. 1.0E-17).

Solution 4 - Php

Simply adding + to your string variable will cause typecast to (float) and removes zeros:

var_dump(+'125.00');     // double(125)
var_dump(+'966.70');     // double(966.7)
var_dump(+'844.011');    // double(844.011)
var_dump(+'844.011asdf');// double(844.011)

Solution 5 - Php

For everyone coming to this site having the same problem with commata instead, change:

$num = number_format($value, 1, ',', '');

to:

$num = str_replace(',0', '', number_format($value, 1, ',', '')); // e.g. 100,0 becomes 100


If there are two zeros to be removed, then change to:

$num = str_replace(',00', '', number_format($value, 2, ',', '')); // e.g. 100,00 becomes 100

More here: https://stackoverflow.com/q/4113200/1066234

Solution 6 - Php

If you want to remove the zero digits just before to display on the page or template.

You can use the sprintf() function

sprintf('%g','125.00');
//125

‌‌sprintf('%g','966.70');
//966.7

‌‌‌‌sprintf('%g',844.011);
//844.011

Solution 7 - Php

You should cast your numbers as floats, which will do this for you.

$string = "42.422005000000000000000000000000";
echo (float)$string;

Output of this will be what you are looking for.

> 42.422005

Solution 8 - Php

$x = '100.10'; 
$x = preg_replace("/\.?0*$/",'',$x); 
echo $x;

There is nothing that can't be fixed with a simple regex ;)

http://xkcd.com/208/

Solution 9 - Php

Typecast to a float.

$int = 4.324000;
$int = (float) $int;

Solution 10 - Php

Be careful with adding +0.

echo number_format(1500.00, 2,".",",")+0;
//1

Result of this is 1.

echo floatval('1,000.00');
// 1

echo floatval('1000.00');
//1000

Solution 11 - Php

Due to this question is old. First, I'm sorry about this.

The question is about number xxx.xx but in case that it is x,xxx.xxxxx or difference decimal separator such as xxxx,xxxx this can be harder to find and remove zero digits from decimal value.

/**
 * Remove zero digits from decimal value.
 * 
 * @param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
 * @param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
 * @return string Return removed zero digits from decimal value.
 */
function removeZeroDigitsFromDecimal($number, $decimal_sep = '.')
{
    $explode_num = explode($decimal_sep, $number);
    if (is_array($explode_num) && isset($explode_num[count($explode_num)-1]) && intval($explode_num[count($explode_num)-1]) === 0) {
        unset($explode_num[count($explode_num)-1]);
        $number = implode($decimal_sep, $explode_num);
    }
    unset($explode_num);
    return (string) $number;
}

And here is the code for test.

$numbers = [
    1234,// 1234
    -1234,// -1234
    '12,345.67890',// 12,345.67890
    '-12,345,678.901234',// -12,345,678.901234
    '12345.000000',// 12345
    '-12345.000000',// -12345
    '12,345.000000',// 12,345
    '-12,345.000000000',// -12,345
];
foreach ($numbers as $number) {
    var_dump(removeZeroDigitsFromDecimal($number));
}


echo '<hr>'."\n\n\n";


$numbers = [
    1234,// 12324
    -1234,// -1234
    '12.345,67890',// 12.345,67890
    '-12.345.678,901234',// -12.345.678,901234
    '12345,000000',// 12345
    '-12345,000000',// -12345
    '12.345,000000',// 12.345
    '-12.345,000000000',// -12.345
    '-12.345,000000,000',// -12.345,000000 STRANGE!! but also work.
];
foreach ($numbers as $number) {
    var_dump(removeZeroDigitsFromDecimal($number, ','));
}

Solution 12 - Php

$str = 15.00;
$str2 = 14.70;
echo rtrim(rtrim(strval($str), "0"), "."); //15
echo rtrim(rtrim(strval($str2), "0"), "."); //14.7

Solution 13 - Php

I found this solution is the best:

public function priceFormat(float $price): string
{
    //https://stackoverflow.com/a/14531760/5884988
    $price = $price + 0;
    $split = explode('.', $price);
    return number_format($price, isset($split[1]) ? strlen($split[1]) : 2, ',', '.');
}

Solution 14 - Php

The following is much simpler

if(floor($num) == $num) {
    echo number_format($num);
} else {
    echo $num;
}

Solution 15 - Php

You can try the following:

rtrim(number_format($coin->current_price,6),'0.')

Solution 16 - Php

Example 1

$value =81,500.00;
{{rtrim(rtrim(number_format($value,2),0),'.')}}

> output > > 81,500

Example 2

$value=110,763.14;
{{rtrim(rtrim(number_format($value,2),0),'.')}}

> output > > 110,763.14

Solution 17 - Php

Complicated way but works:

$num = '125.0100';
$index = $num[strlen($num)-1];
$i = strlen($num)-1;
while($index == '0') {
   if ($num[$i] == '0') {
     $num[$i] = '';
     $i--;
   }

   $index = $num[$i];
}

//remove dot if no numbers exist after dot
$explode = explode('.', $num);
if (isset($explode[1]) && intval($explode[1]) <= 0) {
   $num = intval($explode[0]);
}

echo $num; //125.01

the solutions above are the optimal way but in case you want to have your own you could use this. What this algorithm does it starts at the end of string and checks if its 0, if it is it sets to empty string and then goes to the next character from back untill the last character is > 0

Solution 18 - Php

Thats my small solution... Can included to a class and set vars

private $dsepparator = '.'; // decimals private $tsepparator= ','; // thousand

That can be set by constructor and change to users lang.

class foo
{
    private $dsepparator;
    private $tsepparator;

    function __construct(){
        $langDatas = ['en' => ['dsepparator' => '.', 'tsepparator' => ','], 'de' => ['dsepparator' => ',', 'tsepparator' => '.']];
        $usersLang = 'de'; // set iso code of lang from user
        $this->dsepparator = $langDatas[$usersLang]['dsepparator'];
        $this->tsepparator = $langDatas[$usersLang]['tsepparator'];
    }

    public function numberOmat($amount, $decimals = 2, $hideByZero = false)
    {
    	return ( $hideByZero === true AND ($amount-floor($amount)) <= 0 ) ? number_format($amount, 0, $this->dsepparator, $this->tsepparator) : number_format($amount, $decimals, $this->dsepparator, $this->tsepparator);
    }
    /*
     * $bar = new foo();
     * $bar->numberOmat('5.1234', 2, true); // returns: 5,12
     * $bar->numberOmat('5', 2); // returns: 5,00
     * $bar->numberOmat('5.00', 2, true); // returns: 5
     */

}

Solution 19 - Php

$value = preg_replace('~\.0+$~','',$value);

Solution 20 - Php

You can use:

print (floatval)(number_format( $Value), 2 ) );    

Solution 21 - Php

This is my solution. I want to keep ability to add thousands separator

    $precision = 5;    
    $number = round($number, $precision);
    $decimals = strlen(substr(strrchr($number, '.'), 1));
    return number_format($number, $precision, '.', ',');

Solution 22 - Php

This is a simple one line function using rtrim, save separator and decimal point :

function myFormat($num,$dec)
 {
 return rtrim(rtrim(number_format($num,$dec),'0'),'.');
 }

Solution 23 - Php

Simple and accurate!

function cleanNumber($num){
    $explode = explode('.', $num);
    $count   = strlen(rtrim($explode[1],'0'));
    return bcmul("$num",'1', $count);
}

Solution 24 - Php

Ultimate Solution: The only safe way is to use regex:

echo preg_replace("/\.?0+$/", "", 3.0); // 3
echo preg_replace("/\d+\.?\d*(\.?0+)/", "", 3.0); // 3

it will work for any case

Solution 25 - Php

I use this simple code:

define('DECIMAL_SEPARATOR', ','); //To prove that it works with different separators than "."

$input = "50,00";

$number = rtrim($input, '0');                // 50,00 --> 50,
$number = rtrim($number, DECIMAL_SEPARATOR); // 50,   --> 50

echo $number;

Seems a bit too easy to be the really correct solution, but it works just fine for me. You should do some tests with the inputs you'll be getting before using this.

Solution 26 - Php

Sometimes, especially in case of monetary amounts, you want to remove the zeros only if they are 2, you don't want to print € 2.1 instead of € 2.10.

Some implementations could be:

function formatAmount(string|float|int $value, int $decimals = 2): string
{
    if (floatval(intval($value)) === floatval($value)) {
        // The number is an integer. Remove all the decimals
        return rtrim($value, '0.');
    }

    return number_format($value, $decimals);
}

Or, simpler if $decimals is always 2:

function formatAmount(string|float|int $value): string
{
    return str_replace('.00', '', number_format($value, 2));
}

Examples of expected outputs:

0.1000 => 0.10
2.000 => 2
1.25 => 1.25

Solution 27 - Php

This Code will remove zero after point and will return only two decimal digits.

$number=1200.0000;
str_replace('.00', '',number_format($number, 2, '.', ''));

Output will be: 1200

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvittoView Question on Stackoverflow
Solution 1 - PhplaforView Answer on Stackoverflow
Solution 2 - PhpDiverseAndRemote.comView Answer on Stackoverflow
Solution 3 - PhpmpenView Answer on Stackoverflow
Solution 4 - PhpAlexander YancharukView Answer on Stackoverflow
Solution 5 - PhpAvatarView Answer on Stackoverflow
Solution 6 - PhpHemerson VarelaView Answer on Stackoverflow
Solution 7 - PhpSajan ParikhView Answer on Stackoverflow
Solution 8 - PhpDakkaronView Answer on Stackoverflow
Solution 9 - PhpJosephView Answer on Stackoverflow
Solution 10 - PhpOnionStandView Answer on Stackoverflow
Solution 11 - PhpveeView Answer on Stackoverflow
Solution 12 - PhpRatajSView Answer on Stackoverflow
Solution 13 - PhpRawburnerView Answer on Stackoverflow
Solution 14 - PhpNemrod MondezView Answer on Stackoverflow
Solution 15 - PhpДмитрий СтрижаковView Answer on Stackoverflow
Solution 16 - PhpJudith loboView Answer on Stackoverflow
Solution 17 - PhpGGioView Answer on Stackoverflow
Solution 18 - PhpPaykomanView Answer on Stackoverflow
Solution 19 - PhpNikhil GyanView Answer on Stackoverflow
Solution 20 - PhpMUNEERPASHA GADADView Answer on Stackoverflow
Solution 21 - PhpdespotbgView Answer on Stackoverflow
Solution 22 - PhpAPHView Answer on Stackoverflow
Solution 23 - PhpJ. DoeView Answer on Stackoverflow
Solution 24 - PhpYohanimView Answer on Stackoverflow
Solution 25 - PhpJan ŠtěchView Answer on Stackoverflow
Solution 26 - Phpthe_nutsView Answer on Stackoverflow
Solution 27 - PhpAshish PathakView Answer on Stackoverflow