高端品牌網(wǎng)站制作黃頁推廣2021
在開發(fā)項目的過程中有時候需要進(jìn)行計算百分比,例如計算餅狀圖百分比。有時候在計算的過程中常規(guī)四舍五入計算會發(fā)生所有計算的值相加不等于100%的情況
這是 get_percent_value
函數(shù)的 JavaScript 版本:
/*** 最大余額法,解決百分比計算相加不等于100%(扇形/餅圖百分比使用的此算法)* @param {Array} valueList 二維數(shù)組 [{value: 1}, {value: 2}, {value: 3}]* @param {string} contKey 要統(tǒng)計的字段* @param {number} precision 精度(默認(rèn)為2保留百分比格式的兩位小數(shù))* @param {string} percentKey 百分比鍵名* @param {boolean} format 是否需要返回格式化后百分比格式,false則返回小數(shù)* @return {Array}*/
function getPercentValue(valueList, contKey, precision = 2, percentKey = 'percent', format = true) {if (valueList.length === 0) {return [];}// 求和const sum = valueList.reduce((acc, item) => acc + item[contKey], 0);// 如果總和為0,直接返回if (sum === 0) {return valueList.map(item => ({...item,[percentKey]: format ? '0%' : 0}));}// 計算精度const digits = Math.pow(10, precision);let currentSum = 0;let remainder = [];// 計算每個項目的整數(shù)和余數(shù)部分valueList.forEach((item, index) => {const votesPerQuota = (item[contKey] / sum) * digits * 100;const integerPart = Math.floor(votesPerQuota);valueList[index].integer = integerPart;remainder[index] = votesPerQuota - integerPart;currentSum += integerPart;});const targetSeats = digits * 100;// 找到最大余數(shù)并加1,直到總數(shù)達(dá)到目標(biāo)while (currentSum < targetSeats) {const maxIndex = remainder.indexOf(Math.max(...remainder));valueList[maxIndex].integer++;remainder[maxIndex] = -1; // 確保該余數(shù)不會再被選中currentSum++;}// 生成最終的百分比值valueList.forEach(item => {item[percentKey] = (item.integer / targetSeats).toFixed(precision + 2);if (format) {item[percentKey] = (parseFloat(item[percentKey]) * 100).toFixed(precision) + '%';}delete item.integer;});return valueList;
}// 使用示例
const data = [{ value: 3 },{ value: 3 },{ value: 3 }
];const rateData = getPercentValue(data, 'value', 2, 'percent', false);
console.log(rateData);
說明:
reduce
用于求和。Math.floor
用于獲取整數(shù)部分。Math.max
和indexOf
用于找到最大余數(shù)的位置。toFixed
保留指定的小數(shù)位數(shù)。
你可以通過 getPercentValue
函數(shù)來計算各項的百分比,并決定是否返回格式化的百分比形式。