Gautam Jha
Gautam Jha

Reputation: 1473

trouble with calculating percentage using PHP

I wanted to calculate the percentage using PHP. I tried the code given below but its gives me the return value in float. i don't know much in PHP so please fix this code.

current OUTPUT


66.666666666667%

Expected OUTPUT

66.66%

 <?php 
    $up=4;
    $down:2;
    echo (($ups/($ups+$downs))*100).'%'; 
   ?>

Upvotes: 1

Views: 49

Answers (2)

Jeremie Hirsch
Jeremie Hirsch

Reputation: 477

You can do like this : echo round(66.666666666667, 2); >> 66.66

Upvotes: 1

isnisn
isnisn

Reputation: 254

Use number_format() to specify your decimals and separator.

     <?php 
        $up=4;
        $down:2;
        $num = (($ups/($ups+$downs))*100).'%'; 
        $formatted_num = number_format($num, 2, '.', '');
       echo $formatted_num;
     ?>

Upvotes: 2

Related Questions