一个数字占另一个数字的百分比 [英] Percentage a Number is of Another Number

查看:146
本文介绍了一个数字占另一个数字的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中找到一个数字占另一个数字的百分比?

How to find the percentage a number is of another number in PHP?

示例

$num1 = 2.6;
$num2 = 2.6;
// Should equal to 100%

推荐答案

将两个数字相除并乘以100得到百分比:

Divide the two numbers and multiply by 100 to get the percentage:

$percentage = ($num1 / $num2) * 100;
echo $percentage . "%";

当然,您需要检查以确保$num1不为0,如下所示:$percentage = ($num1 !== 0 ? ($num1 / $num2) : 0) * 100;

Of course you will need to check so that $num1 is not 0, something like this: $percentage = ($num1 !== 0 ? ($num1 / $num2) : 0) * 100;

这篇关于一个数字占另一个数字的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆