当前位置:网站首页>PHP maximum balance method to solve the problem that the sum of the final percentages is not equal to 100
PHP maximum balance method to solve the problem that the sum of the final percentages is not equal to 100
2022-06-12 10:21:00 【Qu Shuai 369】
/**
* Notes: Maximum balance method
* User: Qu Shuai
* DateTime: 2022/2/25 0025 18:12
* @param array $valueList Two dimensional array [['count' => 1],['count' => 2],['count' => 3]]
* @param $contKey string Fields to be counted
* @param $precision int precision
* @param $percentKey int Percentage key name
* @return array
*/
public function getPercentValue(array $valueList, string $contKey, int $precision = 2, $percentKey = 'percent'): array
{
if (count($valueList) === 0) {
return [];
}
$sum = array_sum(array_column($valueList, $contKey));
if ($sum === 0) {
foreach ($valueList as $k => $v) {
$valueList[$k][$percentKey] = 0;
}
return $valueList;
}
$digits = pow(10, $precision);
$currentSum = 0;
$remainder = [];
foreach ($valueList as $k => $v) {
$votesPerQuota = $v[$contKey] / $sum * $digits * 100;
$valueList[$k]['integer'] = (int)$votesPerQuota;
// balance
$remainder[$k] = $votesPerQuota - $valueList[$k]['integer'];
// Integer total ( Will be less than the real total )
$currentSum += $valueList[$k]['integer'];
}
$targetSeats = $digits * 100;
while ($currentSum < $targetSeats) {
$key = array_search(max($remainder), $remainder);
$valueList[$key]['integer']++;
unset($remainder[$key]);
$currentSum++;
}
foreach ($valueList as $k => $v) {
$valueList[$k][$percentKey] = $v['integer'] / $targetSeats;
unset($valueList[$k]['integer']);
}
return $valueList;
}
}边栏推荐
- ServletContext object
- JVM (VI) Virtual machine bytecode execution engine (with stack execution process and bytecode instruction table)
- QT custom window fillets
- Auto. JS learning note 10: instantiate a custom object and use json The stringify() method causes an error (resolved)
- Cookie object
- Access and traversal of string class objects
- one
- 用于图像处理的高性能计算框架
- 数组,整型,字符变量在全局和局部的存在形式
- Halcon combined with C # to detect surface defects -- affine transformation (III)
猜你喜欢

Win10 professional edition user name modification

学生管理系统

机器学习不是你想用,想用就能用

Web3.0与数字时尚,该如何落地?

conda 安装tensorflow 测试tensorflow

How high can C language reach by self-study alone?

3. Abstract Factory

Pycharm view the current version of opencv

Papaya Mobile has a comprehensive layout of cross-border e-commerce SaaS papaya orange. What are the opportunities for this new track?

Circuitbreaker fuse of resilience4j - circuitbreakerevent event
随机推荐
1268_ Implementation of FreeRTOS task context switching
Circuitbreaker fuse of resilience4j - Summary
One test for twoorthree years, recording some thoughts on test exchange experience
[CEGUI] resource loading process
Autojs learning notes 6:text (txt) Findone() will report an error when switching apps. Finally, solve the implementation effect and switch any app until the script finds the control with the specified
Add jar package under idea2018 web project
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
[MySQL] index invalidation and index optimization
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
性能指标的信仰危机
[Wayland] Weston startup process analysis
[CEGUI] window environment compilation
远程桌面不能复制粘贴解决办法
How high can C language reach by self-study alone?
古董级MFC/GDI+框架LCD显示控件
Class selectors and using pseudo class selectors with
[CEGUI] concept introduction
Yarn scheduling
Jetpack architecture component learning (3) -- activity results API usage
Win10 professional edition user name modification