Joeeee Joeeee - 1 month ago 5
PHP Question

php percent calculation

how to implement calculation of percents:

$x=3;
$y = 100\$x; // 3.333..
$x*$y - and here I need to get 100 without Observational error


Any ideas?

Answer

Your backslash is not a division symbol. You must use division / symbol. Here's an example using number_format() to round to the hundredths position.

$x = 3;
$y = 15;

$percent = $x/$y;
$percent_friendly = number_format( $percent * 100, 2 ) . '%';
Comments