js 数字金额转为大写 js 金额转大写

一、金额转大写文字

javascript 复制代码
ChineseWords.js文件

const ChineseWords = (value) => {
  return currencyToChinese(value, {
    currency: false
  })
}

const CN_NUMBERS = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const CN_UNITS = ['', '拾', '佰', '仟'];
const CN_SECTION_UNITS = ['', '万', '亿', '万亿'];
const CN_DECIMAL_UNITS = ['角', '分'];

/**
 * 将金额转换为中文
 * @param {number|string} amount 金额,可以是数字或字符串
 * @param {object} options 选项对象
 * @param {boolean} options.currency 是否添加人民币字样
 * @returns {string} 中文金额表示
 */
const currencyToChinese = (
  amount,
  options
) => {
  if (amount === null || amount === undefined || amount === '') {
    return options.currency ? '人民币零元整' : '零元整';
  }

  // 转换为数字并验证
  const num = typeof amount === 'string' ? parseFloat(amount.replace(/,/g, '')) : amount;

  if (isNaN(num)) {
    throw new Error('无效的金额格式');
  }

  if (Math.abs(num) >= 999999999999.99) {
    throw new Error('金额超出处理范围(最大支持9999亿)');
  }

  // 处理负数
  const isNegative = num < 0;
  const absAmount = Math.abs(num);

  // 分离整数和小数部分
  const [integerPart, decimalPart] = absAmount.toFixed(2).split('.');

  let result = '';

  // 处理整数部分
  if (integerPart !== '0') {
    result += convertInteger(integerPart);
  }

  // 处理小数部分
  if (decimalPart && decimalPart !== '00') {
    if (result) {
      result += '元';
    }
    result += convertDecimal(decimalPart);
  } else {
    if (result) {
      result += '元整';
    } else {
      result = '零元整';
    }
  }

  // 添加人民币字样
  if (options.currency) {
    result = '人民币' + result;
  }

  // 添加负号
  if (isNegative) {
    result = '负' + result;
  }

  return result;
}

/**
 * 将整数转换为中文
 * @param {string} integerStr 整数字符串
 */
const convertInteger = (integerStr) => {
  const num = parseInt(integerStr, 10);
  if (num === 0) return '';

  let result = '';
  const str = num.toString();
  const len = str.length;
  let zeroCount = 0;

  for (let i = 0; i < len; i++) {
    const n = parseInt(str[i], 10);
    const pos = len - i - 1;
    const section = Math.floor(pos / 4);
    const unitPos = pos % 4;

    if (n === 0) {
      zeroCount++;
    } else {
      if (zeroCount > 0) {
        result += '零';
        zeroCount = 0;
      }
      result += CN_NUMBERS[n] + CN_UNITS[unitPos];
    }

    // 处理万、亿等单位
    if (unitPos === 0 && section > 0) {
      if (zeroCount < 4) {
        result += CN_SECTION_UNITS[section];
      }
      zeroCount = 0;
    }
  }
  return result;
}

/** 
 * 将小数部分转换为中文
 * @param {string} decimalStr 小数字符串
*/
const convertDecimal = (decimalStr) => {
  let result = '';

  for (let i = 0; i < decimalStr.length && i < 2; i++) {
    const n = parseInt(decimalStr[i], 10);
    if (n !== 0) {
      result += CN_NUMBERS[n] + CN_DECIMAL_UNITS[i];
    }
  }
  return result || '';
}
/** 
 * 将金额转换为中文
 * @param {number|string} amount 金额,可以是数字或字符串
*/
const amountToChinese = (amount) => {
  return currencyToChinese(amount, { currency: false });
}
/**
 * 验证金额是否有效
 * @param {number|string} amount 金额,可以是数字或字符串
 * @returns {boolean} 是否有效
 */
const validateAmount = (amount) => {
  if (amount === null || amount === undefined || amount === '') {
    return false;
  }

  const num = typeof amount === 'string' ? parseFloat(amount.replace(/,/g, '')) : amount;
  return !isNaN(num) && Math.abs(num) < 999999999999.99;
}

/**
 * 格式化金额
 * @param {number|string} amount 金额,可以是数字或字符串
 * @returns {string} 格式化后的金额
 */
const formatAmount = (amount) => {
  if (amount === null || amount === undefined || amount === '') {
    return '0.00';
  }

  const num = typeof amount === 'string' ? parseFloat(amount.replace(/,/g, '')) : amount;
  if (isNaN(num)) return '0.00';

  return num.toLocaleString('zh-CN', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  });
}
export default ChineseWords

二、页面中使用

javascript 复制代码
import ChineseWords from "@/views/ChineseWords.js"; // 转大写文字js
// 监听汇款金额输入,实时转换为大写
const handleAmountInput = () => {
  // 转换为大写并赋值
  ruleForm.value.amountUpper = ChineseWords(amount);
};
相关推荐
UpgradeLink1 分钟前
基于 Go 打造的升级链路管理平台:upgradelink 让设备升级更简单
开发语言·后端·golang
天府之绝3 分钟前
uniapp 中使用uview表单验证时,自定义扩展的表单,在改变时无法触发表单验证处理;
开发语言·前端·javascript·vue.js·uni-app
be or not to be4 分钟前
Html-CSS动画
前端·css·html
初恋叫萱萱9 分钟前
技术基石与职场进阶:构建从Web后端到高性能架构的完整知识图谱
前端·架构·知识图谱
萧鼎13 分钟前
深入解析 Python 的 Word 模板引擎:docxtpl 全面指南
开发语言·python·word
木木木一13 分钟前
Rust学习记录--C9 错误处理
前端·学习·rust
Chan1614 分钟前
场景题:如何设计一个分布式ID
java·开发语言·spring boot·java-ee·intellij-idea
局外人LZ15 分钟前
libsodium.js:web端与 Node.js 的现代加密工具集,构建前端安全加密体系
前端·javascript·node.js
chamu9920 分钟前
C++ 的可调用对象
开发语言·c++
xkxnq20 分钟前
第二阶段:Vue 组件化开发(第 20天)
前端·javascript·vue.js