JS数字处理函数 - 保留小数、货币格式、中文大写转换

| 函数名称 | 函数描述 | 参数 | 返回值 | | --------------- | ---------------------------------------------------------------- | ------------------------------------------ | ----------- | | roundNumber | 将数字保留指定位数小数 | num: 要处理的数字, digits: 要保留的小数位数 | 处理后的数字 | | formatCurrency | 将大数字转换为货币格式 | number: 要转换的数字 | 货币格式字符串 | | numberToChinese | 将数字转换为中文大写 | num: 要转换的数字,范围在[0, 999999999]之间 | 中文字符串 | | numberToChinese1 | 将数字转换为中文大写(采用'壹、贰、叁'等形式表示) | num: 要转换的数字,范围在[0, 999999999]之间 | 中文字符串 |

/**
 * 将数字保留指定位数小数
 * @param {number} num - 要处理的数字
 * @param {number} [digits=2] - 要保留的小数位数,默认为2
 * @returns {number} - 处理后的数字
 */
export function roundNumber(num, digits = 2) {
   // 如果num不是数字,则直接返回0
   if (typeof num !== 'number') {
      return 0;
   }

   // 使用Math.round方法将num四舍五入到指定位数,并使用parseFloat方法将结果转换为浮点数
   const factor = Math.pow(10, digits);
   const roundedNum = Math.round(num * factor) / factor;

   return parseFloat(roundedNum.toFixed(digits));
}



/**
 * 将大数字转换为货币格式
 * @param {number} number - 要转换的数字
 * @returns {string} - 货币格式字符串
 */
export function formatCurrency(number) {
   // 如果number不是数字,则返回空字符串
   if (typeof number !== 'number') {
      return '';
   }

   // 定义单位数组和对应的数字值
   const units = ['元', '万', '亿'];
   const numbers = [1, 10000, 100000000];

   // 循环遍历units数组,根据number的大小确定当前最大的单位,并进行相应的处理
   for (let i = units.length - 1; i >= 0; i--) {
      const unit = units[i];
      const digits = numbers[i];

      if (number < digits) {
         continue;
      }

      const result = number / digits;

      // 如果数值小于10万,保留2位小数;否则保留0位小数
      const decimalDigits = result < 100000 ? 2 : 0;
      const formattedResult = result.toFixed(decimalDigits);

      return `${formattedResult}${unit}`;
   }

   // 如果number过小,直接返回原数字
   return number.toString();
}



/**
 * 将数字转换为中文大写
 * @param {number} num - 要转换的数字,范围在[0, 999999999]之间
 * @returns {string} - 转换后的中文字符串
 */
export function numberToChinese(num) {
   const units = ['', '十', '百', '千'];
   const digits = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
   const bigUnits = ['', '万', '亿'];

   if (typeof num !== 'number' || num < 0 || num > 999999999) {
      return '';
   }

   const numString = num.toString();
   const numLength = numString.length;
   let result = '';

   for (let i = 0; i < numLength; i++) {
      const digit = parseInt(numString[i]);
      const unit = units[numLength - i - 1];
      const bigUnit = bigUnits[Math.floor(i / 4)];

      // 如果当前数字为0,则在结果字符串中添加'零'
      if (digit === 0) {
         // 如果上一位已经是0,则不重复添加
         if (result.slice(-1) !== '零') {
            result += digits[digit];
         }
      } else {
         result += digits[digit] + unit;
      }

      // 如果当前位置是万或亿的边界,并且后面还有数字,则添加相应的大单位
      if (bigUnit && (i + 1) % 4 === 0 && numLength - i - 1 >= 4) {
         result += bigUnit;
      }
   }

   return result;
}



/**
 * 将数字转换为中文大写(采用'壹、贰、叁'等形式表示)
 * @param {number} num - 要转换的数字,范围在[0, 999999999]之间
 * @returns {string} - 转换后的中文字符串
 */
export function numberToChinese1(num) {
   const units = ['', '拾', '佰', '仟'];
   const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
   const bigUnits = ['', '万', '亿'];

   if (typeof num !== 'number' || num < 0 || num > 999999999) {
      return '';
   }

   const numString = num.toString();
   const numLength = numString.length;
   let result = '';

   for (let i = 0; i < numLength; i++) {
      const digit = parseInt(numString[i]);
      const unit = units[numLength - i - 1];
      const bigUnit = bigUnits[Math.floor(i / 4)];

      // 如果当前数字为0,则在结果字符串中添加'零'
      if (digit === 0) {
         // 如果上一位已经是0,则不重复添加
         if (result.slice(-1) !== '零') {
            result += digits[digit];
         }
      } else {
         result += digits[digit] + unit;
      }

      // 如果当前位置是万或亿的边界,并且后面还有数字,则添加相应的大单位
      if (bigUnit && (i + 1) % 4 === 0 && numLength - i - 1 >= 4) {
         result += bigUnit;
      }
   }

   return result;
}
JS数字处理函数 - 保留小数、货币格式、中文大写转换

原文地址: https://www.cveoy.top/t/topic/lLgK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录