Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP计算百分比_Php_Percentage - Fatal编程技术网

PHP计算百分比

PHP计算百分比,php,percentage,Php,Percentage,我需要一些帮助。这是一个简单的代码,但我不知道如何写下来。我有电话号码: $NumberOne = 500; $NumberTwo = 430; $NumberThree = 150; $NumberFour = 30; 总之,这是: $Everything = 1110; // all added 现在我想展示一下百分比是多少,例如$NumberFour占所有内容的百分比,或者$numberwo占所有内容的百分比。“市场份额”也是如此 使用一些简单的数学:将你想要找到百分比的数字除以总数

我需要一些帮助。这是一个简单的代码,但我不知道如何写下来。我有电话号码:

$NumberOne = 500;
$NumberTwo = 430;
$NumberThree = 150;
$NumberFour = 30; 
总之,这是:

$Everything = 1110; // all added

现在我想展示一下百分比是多少,例如$NumberFour占所有内容的百分比,或者$numberwo占所有内容的百分比。“市场份额”也是如此

使用一些简单的数学:将你想要找到百分比的数字除以总数,再乘以100

例如:

$total = 250;
$portion = 50;
$percentage = ($portion / $total) * 100; // 20
原始示例的解决方案 要获得四个$NumberFour占您使用总额的百分比,请执行以下操作:

$percentage = ($NumberFour / $Everything) * 100;
四舍五入 根据您使用的数字,您可能希望对结果百分比进行四舍五入。在我最初的例子中,我们得到20%,这是一个很好的整数。然而,原始问题使用1110作为总数,30作为数字来计算(2.70270…)的百分比

PHP内置的
round()
函数在使用百分比进行显示时非常有用:

辅助函数 我只考虑在助手函数的使用合理时创建它们(如果计算和显示百分比不是一次性的)。我在下面附上了一个例子,将上面的所有内容联系在一起

function format_percentage($percentage, $precision = 2) {
    return round($percentage, $precision) . '%';
}

function calculate_percentage($number, $total) {

    // Can't divide by zero so let's catch that early.
    if ($total == 0) {
        return 0;
    }

    return ($number / $total) * 100;
}

function calculate_percentage_for_display($number, $total) {
    return format_percentage(calculate_percentage($number, $total));
}

echo calculate_percentage_for_display(50, 250); // 20%
echo calculate_percentage_for_display(30, 1110); // 2.7%
echo calculate_percentage_for_display(75, 190); // 39.47%

创建函数以计算两个数字之间的百分比

<?php

/**
 * Calculate percetage between the numbers
 */

function percentageOf( $number, $everything, $decimals = 2 ){
    return round( $number / $everything * 100, $decimals );
}

$numbers = array( 500, 430, 150, 30 );
$everything = array_sum( $numbers );

echo 'First of everything: '.percentageOf( $numbers[0], $everything )."%\n";
echo 'Second of everything: '.percentageOf( $numbers[1], $everything )."%\n";
echo 'Third of everything: '.percentageOf( $numbers[2], $everything )."%\n";
echo 'Fourth of everything: '.percentageOf( $numbers[3], $everything )."%\n";

?>

你试过什么吗?如果你发现你在给变量编号,那么你可能应该使用数组而不是提示:百分比是
(部分/全部)*100
$numbers=array(500200500200100);对于此数组,总百分比和不等于100,而是99.99。它始终为.01shortage@RockersTechnology视情况而定。可能是的,因为它四舍五入到两位小数。你总是可以显示更多的小数
<?php

/**
 * Calculate percetage between the numbers
 */

function percentageOf( $number, $everything, $decimals = 2 ){
    return round( $number / $everything * 100, $decimals );
}

$numbers = array( 500, 430, 150, 30 );
$everything = array_sum( $numbers );

echo 'First of everything: '.percentageOf( $numbers[0], $everything )."%\n";
echo 'Second of everything: '.percentageOf( $numbers[1], $everything )."%\n";
echo 'Third of everything: '.percentageOf( $numbers[2], $everything )."%\n";
echo 'Fourth of everything: '.percentageOf( $numbers[3], $everything )."%\n";

?>
First of everything: 45.05%
Second of everything: 38.74%
Third of everything: 13.51%
Fourth of everything: 2.7%