Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percent from decimal? PHP

Tags:

php

Trying to convert this "0.274509817" to a nice precentage like 27%

The string is a dynamic value from an API.

like image 211
AndrewFerrara Avatar asked Feb 20 '11 05:02

AndrewFerrara


People also ask

How can I get percentage in PHP?

PHP program to Calculate Percentage php // defining function function cal_percentage($num_amount, $num_total) { $count1 = $num_amount / $num_total; $count2 = $count1 * 100; $count = number_format($count2, 0); return $count; } // calling function to calculate percentage echo "Percentage of 39 in 100 : ".

How do I round to 2 decimal places in PHP?

Example #1 round() examples php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?>

Does PHP have decimal?

How do you check if number is decimal or not PHP? The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.


1 Answers

$percent = round((float)$str * 100 ) . '%';

Where $str = "0.274509817"

like image 94
Alec Gorge Avatar answered Oct 04 '22 20:10

Alec Gorge