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

我们通常使用三种书面数字系统:全球使用的阿拉伯数字系统和两种本地数字系统(繁体、简体)。常规时我们使用阿拉伯数字(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 || '';
}

以上就是实现过程。

js将数字金额转换成中文金额格式的更多相关文章

  1. JS把数字金额转换成中文大写数字的函数

    //把数字金额转换成中文大写数字的函数 function num2rmb ($num){ $c1="零壹贰叁肆伍陆柒捌玖"; $c2="分角元拾佰仟万拾佰仟亿" ...

  2. PHP 数字金额转换成中文大写金额的函数 数字转中文

    /** *数字金额转换成中文大写金额的函数 *String Int $num 要转换的小写数字或小写字符串 *return 大写字母 *小数位为两位 **/ function num_to_rmb($ ...

  3. c#金额转换成中文大写金额

    2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param ...

  4. c#金额转换成中文大写金额 .Net开发Windows服务

    c#金额转换成中文大写金额   2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> ...

  5. 在C#中将金额转换成中文大写金额

    具体代码如下: /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param name="LowerMoney ...

  6. java阿拉伯数字表示的金额转换成中文大写金额

    最大数字要处理到千亿也就是12位整数部分我们可以分成3段处理,xxxx亿,xxxx万,xxxx元,然后小数部分比较好处理我们发现0比较难处理什么时候会出现零呢那就是两个数字之间出现一个或多个零那么数字 ...

  7. excel小写金额转换成中文大写

    假设 假设数据在A1单元格 任何一个个单元格输入公式=TEXT(INT(A1),"[dbnum2]")&"元"&IF(INT(A1*10)-IN ...

  8. 在C#中将数字转换成中文

    上篇我们讲了在MSSQL中将数字转换成中文,这篇我们讲讲在C#中将数字转换成中文 下篇将讲一下如何将金额转换成中文金额,废话不多说,具体代码如下: /// <summary> /// 数字 ...

  9. python初学者笔记(2):阿拉伯数字转换成中文大写

    题:输入一个数字,转换成中文大写的写法 可运行的程序(Python 2.7.9): # -*- coding: utf-8 -*- #在python2的py文件里面写中文,必须要添加一行声明文件编码的 ...

  10. JavaScript将输入的数字金额转换成对应的中文大写金额

    // 将输入的数字金额转换成对应的中文大写金额 // idNumber输入的数字金额,idCHN输出的中文大写金额 function TransformNumberIntoCHN(idNumber, ...

随机推荐

  1. TypeScript 学习笔记 — 数组常见的类型转换操作记录(十四)

    获取长度 length type LengthOfTuple<T extends any[]> = T["length"]; type A = LengthOfTupl ...

  2. linux高级编程之线程间的通信(pthread_cleanup_push和pthread_cleanup_pop)

    linux高级编程之线程间的通信(pthread_cleanup_push和pthread_cleanup_pop) 线程可以安排他退出时需要调用的函数,这与进程可以用atexit函数安排进程退出时需 ...

  3. [Pytorch框架] 1.1、Pytorch简介

    文章目录 1.1 Pytorch 简介 1.1.1 PyTorch的由来 1.1.2 Torch是什么? 1.1.3 重新介绍 PyTorch 1.1.4 对比PyTorch和Tensorflow 1 ...

  4. ai问答:使用 Vue3 组合式API 和 TS 父子组件共享数据

    这是一个使用 Vue3 组合式 API 和 TypeScript 的简单父子组件共享数据示例 父组件 Parent.vue: <template> <div> <p> ...

  5. Python从零到壹丨详解图像锐化Roberts、Prewitt算子实现边缘检测

    摘要:图像锐化和边缘提取技术可以消除图像中的噪声,提取图像信息中用来表征图像的一些变量,为图像识别提供基础.本章主要介绍Robert算子.Prewitt算子.Sobel算子.Laplacian算子.S ...

  6. Python对两个Excel操作

    简介 现在有个需求,我们根据需要 data.xlsx 中某些单元格的内容来查找 find.xlsx 中的某些內容. 数据内容(为了数据安全,所有数据均已模糊处理) data.xlsx内容: find. ...

  7. CANoe _ DBC 的创建过程

    在Canoe中创建DBC(Database Container)文件,用于描述和定义CAN总线上的节点.消息和信号,遵循以下步骤: 1.打开Canoe 启动Canoe软件. 2.创建新项目 在Cano ...

  8. Vue_Django 登录注册+图书管理系统

    Vue前端 注册页面 点击查看代码 <template> <div class="register"> <el-row :gutter="2 ...

  9. 2023-06-15:说一说Redis的Key和Value的数据结构组织?

    2023-06-15:说一说Redis的Key和Value的数据结构组织? 答案2023-06-15: 全局哈希表 Redis使用哈希表作为保存键值对的数据结构,通过哈希函数将Key映射为哈希表中的一 ...

  10. JetBrain学信网注册(Clion)

    一.打开网站 首先打开JetBrains关于学生认证的网站:https://www.jetbrains.com/shop/eform/students,可以看见以下页面: 二.人工验证 人工验证适合于 ...