Thursday, April 7, 2016

Add Decimal in your amount at desire position

Code

$amount = 560;
$decimalPosition = 2;
$divideBy   =   pow(10, $decimalPosition );
$newNumber  =   number_format( $totalTax / $divideBy , 2, '.','');
echo $newNumber;

OutPut

5.60


Second Way

        $decimal_point = 2;
        $totalTax=1260; //1.260

        $a=substr($totalTax,0,-$decimal_point); //560 ka 5
        $b=substr($totalTax,-$decimal_point,$decimal_point); //560 ka 60
        
        $c = $a.'.'.$b;
        echo $c;
        


Third Way

        /********* in one line ****************************************\
        
        $d = (substr($totalTax,0,-$decimal_point)).'.'.(substr($totalTax,-$decimal_point,$decimal_point));
        echo $d;