UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

40 lines (39 loc) 1.4 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPercentString = exports.getPercent = void 0; /** 获取某个值占总数的百分比 */ function getPercent(divider, total, decimals) { if (decimals === void 0) { decimals = 2; } if (!total || !divider) { return '0%'; } var value = divider / total; return getPercentString(value, decimals); } exports.getPercent = getPercent; /** e.g. 将30转换成 30% */ function getPercentString(value, decimals) { if (decimals === void 0) { decimals = 2; } var value100 = value * 100; var value100String = value100.toString(); var _a = __read(value100String.split('.'), 2), decimalsPart = _a[1]; var needFixed = !!(decimalsPart && decimalsPart.length > decimals); return (needFixed ? value100.toFixed(decimals) : value100) + '%'; } exports.getPercentString = getPercentString;