1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import accounting from "accounting";
- const setIntervalImmediately = (fn, duration) =>
- setInterval((() => (fn(), fn))(), duration);
- function formatImgSrc(srcArr) {
- if (Array.isArray(srcArr) && srcArr.length > 0) {
- return srcArr[0] ?? "";
- }
- return "";
- }
- const isEmptyValue = (value) => {
- if (value == null) return true
- if (Array.isArray(value) && value.length === 0) return true
- if (typeof value === 'object' && Object.keys(value).length === 0) return true
- return false
- }
- const formatNumber = (n) => {
- let num = Number(n ?? '')
- // 如果数字大于或等于10,000万,则显示为9999w+
- if (num >= 10000 * 10000) {
- return '9999万+'
- }
- // 如果数字大于或等于1万,则转换为以“万”为单位,不四舍五入,最多保留两位小数
- else if (num >= 10000) {
- let w = Math.floor(num / 10000) // 取整,避免四舍五入
- let remainder = num % 10000
- // 计算小数部分并限制为最多两位
- let decimalPart = ''
- if (remainder > 0) {
- // 将余数转换为两位小数,但不进行四舍五入
- decimalPart = ('.' + Math.floor(remainder / 100)).slice(0, 2)
- // 移除结尾的0,如果有的话
- decimalPart = decimalPart.replace(/\.?0+$/, '')
- }
- return `${w}${decimalPart}万`
- }
- // 对于小于1万的数字直接输出
- else {
- return accounting.formatNumber(num)
- }
- }
- export { setIntervalImmediately, formatImgSrc, formatNumber, isEmptyValue };
|