js将数字金额转换成中文金额格式

这篇具有很好参考价值的文章主要介绍了js将数字金额转换成中文金额格式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在开发中我们经常会遇到处理数字的问题,下面介绍一种处理数字金额转换为中文金额的方式:

我们通常使用三种书面数字系统:全球使用的阿拉伯数字系统和两种本地数字系统(繁体、简体)。常规时我们使用阿拉伯数字(1,2,3等),但在某些情况中,如金融中我们会使用繁体汉字来书写数字,繁体字优点是安全且无法篡改,弥补了阿拉数字易于修改的问题,如在银行帐户存储或支票上使用繁体大写数字。 

中文大写书写时的注意事项:

中文大写金额数字应用正楷或行书填写,使用繁体字。如壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等字样。

一、中文大写金额数字到"元"为止的,在"元"之后,应写"整"(或"正")字,在"角"之后,可以不写"整"(或"正")字。

二、中文大写金额数字前应标明"人民币"字样,大写金额数字有"分"的,"分"后面不写"整"(或"正")字。

三、大写金额数字应紧接"人民币"字样填写,不得留有空白。大写金额数字前未印"人民币"字样的,应加填"人民币"三字。在票据和结算凭证大写金额栏内不得预印固定的"仟、佰、拾、万、仟、佰、拾、元、角、分"字样。

四、阿拉伯数字小写金额数字中有"0"时,中文大写应按照汉语语言规律、金额数字构成和防止涂改的要求进行书写。

以下是具体代码实现:

/**
 * convertCurrencyToChinese - 数字转成汉字
 * @params num === 要转换的数字
 * @return 汉字
 * 
*/
function convertCurrencyToChinese(num) { if(!num){ return '零'; } // Constants: const MAXIMUM_NUMBER = 99999999999.99; // Predefine the radix characters and currency symbols for output: const CN_ZERO = "零"; const CN_ONE = "壹"; const CN_TWO = "贰"; const CN_THREE = "叁"; const CN_FOUR = "肆"; const CN_FIVE = "伍"; const CN_SIX = "陆"; const CN_SEVEN = "柒"; const CN_EIGHT = "捌"; const CN_NINE = "玖"; const CN_TEN = "拾"; const CN_HUNDRED = "佰"; const CN_THOUSAND = "仟"; const CN_TEN_THOUSAND = "万"; const CN_HUNDRED_MILLION = "亿"; // const CN_SYMBOL = "人民币"; const CN_DOLLAR = "元"; const CN_TEN_CENT = "角"; const CN_CENT = "分"; const CN_INTEGER = "整"; // Variables: // let integral; // Represent integral part of digit number. // let decimal; // Represent decimal part of digit number. let outputCharacters; // The output result. // let parts; // let digits; // let radices; // let bigRadices; // let decimals; let zeroCount; let i; let p; let d; let quotient; let modulus; let currencyDigits = num; // Validate input string: currencyDigits = currencyDigits.toString(); if (currencyDigits === "") { // alert("Empty input!"); return ""; } if (currencyDigits.match(/[^,.\d]/) != null) { // alert("Invalid characters in the input string!"); return ""; } if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) { // alert("Illegal format of digit number!"); return ""; } // Normalize the format of input digits: currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters. currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning. // Assert the number is not greater than the maximum number. if (Number(currencyDigits) > MAXIMUM_NUMBER) { // eslint-disable-next-line no-console console.warn("输入的金额太大,请重新输入!"); return ""; } // Process the coversion from currency digits to characters: // Separate integral and decimal parts before processing coversion: const parts = currencyDigits.split("."); // eslint-disable-next-line prefer-const let [integral, decimal = ''] = parts; if (parts.length > 1) { // Cut down redundant decimal digits that are after the second. decimal = decimal.substr(0, 2); } // Prepare the characters corresponding to the digits: const digits = [CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE]; const radices = ["", CN_TEN, CN_HUNDRED, CN_THOUSAND]; const bigRadices = ["", CN_TEN_THOUSAND, CN_HUNDRED_MILLION]; const decimals = [CN_TEN_CENT, CN_CENT]; // Start processing: outputCharacters = ""; // Process integral part if it is larger than 0: if (Number(integral) > 0) { zeroCount = 0; for (i = 0; i < integral.length; i++) { p = integral.length - i - 1; d = integral.substr(i, 1); quotient = p / 4; modulus = p % 4; if (d === "0") { zeroCount++; } else { if (zeroCount > 0) { outputCharacters += digits[0]; } zeroCount = 0; outputCharacters += digits[Number(d)] + radices[modulus]; } if (modulus === 0 && zeroCount < 4) { outputCharacters += bigRadices[quotient]; } } outputCharacters += CN_DOLLAR; } // Process decimal part if there is: if (decimal !== "") { for (i = 0; i < decimal.length; i++) { d = decimal.substr(i, 1); if (d !== "0") { outputCharacters += digits[Number(d)] + decimals[i]; } } } // Confirm and return the final output string: if (outputCharacters === "") { outputCharacters = CN_ZERO + CN_DOLLAR; } if (decimal === "") { outputCharacters += CN_INTEGER; } return outputCharacters || ''; }

以上就是实现过程。文章来源地址https://www.toymoban.com/news/detail-604998.html

到了这里,关于js将数字金额转换成中文金额格式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理

    感谢小伙伴儿的分享: ● 不羁 ● 郭中天 整合调整后的工具类Gitee地址:https://gitee.com/yuanzhengme/java_application_aspose_demo ● WordToPdfUtil用于将word文档转换为pdf格式的工具类 ● ExcelToPdfUtil用于将excel文档转换为pdf格式的工具类 ● PdfToImageUtil用于将pdf文档转换为image格式的工具类

    2024年01月24日
    浏览(60)
  • JS实现阿拉伯数字转中文

    这个函数首先定义了两个数组,一个用于存储 0 到 9 的中文字符,另一个用于存储各个位的单位(如十、百、千等)。然后,它使用一个循环,从个位开始处理数字,每次处理一位。如果当前位是 0,它会检查前一位是否也是 0,如果不是,则在结果字符串中添加一个 “零”

    2024年02月13日
    浏览(41)
  • visual studio代码解析(注释)英文换成中文包

    前文:我们用visual studio看别人代码或者看函数不知道意思的时候,看官方提示,又是全英文看不懂,这种情况换成中文就会很大提高代码书写效率,大家也可以去看官方文档是怎么教我们做的https://docs.microsoft.com/zh-cn/dotnet/core/install/localized-intellisense 下载中文包 解压后得到下

    2024年02月13日
    浏览(42)
  • EasyExcel3.0读(日期、数字或者自定义格式转换)

    依赖 对象 test_number.xlsx

    2024年02月09日
    浏览(29)
  • jquery 数字金额转化为大写金额

    示例:money:100 转化为 壹佰元整 //大写金额元币 function DaXieJinE(money) {     // 汉字的数字     var cnNums = new Array(\\\'零\\\', \\\'壹\\\', \\\'贰\\\', \\\'叁\\\', \\\'肆\\\', \\\'伍\\\', \\\'陆\\\', \\\'柒\\\', \\\'捌\\\', \\\'玖\\\');     // 基本单位     var cnIntRadice = new Array(\\\'\\\', \\\'拾\\\', \\\'佰\\\', \\\'仟\\\');     // 对应整数部分扩展单位     var cnIn

    2024年04月12日
    浏览(26)
  • 在js中常见的时间格式及其转换

    在计算机编程中,常见的时间格式有以下几种: 1:ISO 8601 格式:国际标准的日期和时间表示方法。 格式为 “YYYY-MM-DDTHH:mm:ss.sssZ”,其中 “T” 是日期和时间的分隔符,“Z” 表示时区。 例如,“2023-09-29T12:34:56Z” 表示 2023 年 9 月 29 日 12 时 34 分 56 秒的时间点。 2:日期字符

    2024年02月07日
    浏览(29)
  • js时间格式化和转换的方法

    近期在练习或写项目时经常会遇到时间格式的转换问题,今天我就来总结一下。 1、将日期转换为指定格式( yyyy-MM-dd hh:mm:ss 等格式) 封装方法format 也可以为Date原型直接添加format方法 2.将时间戳转换为年月日的格式 或者获取到date之后结合format使用 3.将时间转换为时间戳 注

    2024年02月11日
    浏览(28)
  • JavaScript 格式化金额

    这是 JavaScript 中格式化金额的最常见方法。 Intl.NumberFormat() 构造函数接受两个参数:语言环境和选项。语言环境是为其格式化金额的语言和地区。选项是一组控制金额格式的属性。例如,可以使用样式属性来指定货币的格式,使用货币属性来指定要将金额格式化为的货币。

    2024年02月06日
    浏览(32)
  • 【js】时间和时间戳转换、日期格式化

    1、时间戳转换日期方法 (格式:2023-08-17) 2、日期字符串转时间戳 3、时间戳转换日期+时间方法 date:时间戳数字(格式:2023-08-17 14:11:01) 4、 获取日期中文格式

    2024年02月12日
    浏览(40)
  • 【Java LocalDateTime】LocalDateTime获取时间信息、格式化、转换为数字时间戳

    文章目录 正文         一、描述         二、基本使用 1、获取LocalDateTime时间 2、时间比较 3、获取基本时间信息: 4、格式化 / 反格式化 5、转换为数字时间戳 6、数字时间戳转为LocalDateTime         LocalDateTime是Java 8引入的日期和时间API (java.time包)中的一个类, 不包含

    2024年02月03日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包