当前位置:网站首页>Common formatting methods for amount numbers
Common formatting methods for amount numbers
2022-06-25 14:18:00 【jason_ renyu】
Common formatting methods for amount numbers , The conversion between thousandths and Chinese characters is provided here
Number amount formatting method
/**
* Numeric amount formatting ( Thousandths )
* @param {Number} money Amount number to convert
* @param {Number} num Significant digits after the decimal point
* @returns
*/
const moneyFormatter = (money, num) => {
num = num > 0 && num <= 20 ? num : 2;
money = parseFloat((money + '').replace(/[^\d.-]/g, '')).toFixed(num) + '';
let l = money.split('.')[0].split('').reverse(), r = money.split('.')[1];
let t = '';
for (let i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 === 0 && i + 1 != l.length ? ',' : '');
}
return t.split('').reverse().join('') + '.' + r;
}
Convert the numerical amount to the amount in Chinese characters
/**
* Digital amount to Chinese amount
* @param {Number} money Digital amount
* @returns Amount in Chinese characters
*/
const moneyToUpper = (money = 0) => {
// The number of Chinese characters
const cnNums = [' zero ', ' one ', ' Ii. ', ' 3 ', ' boss ', ' wu ', ' lu ', ' Retailer, ', ' ', ' nine '];
// Basic unit
const cnIntRadice = ['', ' Ten ', ' Bai ', ' Thousand '];
// Corresponding integer part extension unit
const cnIntUnits = ['', ' ten thousand ', ' Billion ', ' mega '];
// Corresponding to the unit of piglet
const cnDecUnits = [' horn ', ' branch ', ' Milli ', ' Li '];
// The character following the integer amount
const cnInteger = ' whole ';
// The unit at the end of an integer
const cnIntLast = ' round ';
// Maximum processed number
const maxNum = 99999999999999.9999;
// Integer part of amount
let integerNum,
// Decimal part of amount
decimaNUm,
// Output Chinese amount string
chineseStr = '',
// Array used after separating amounts , predefined
parts;
if (money === '') {
return '';
}
money = parseFloat(money);
if (money >= maxNum) {
// Maximum processing number exceeded
return ' Maximum processing number exceeded ';
}
if (money === 0) {
chineseStr = cnNums[0] + cnIntLast + cnInteger;
return chineseStr;
}
// Convert to string
money = money.toString();
if (money.indexOf('.') === -1) {
integerNum = money;
decimaNUm = '';
} else {
parts = money.split('.');
integerNum = parts[0];
// The maximum number after the decimal point is 4
decimaNUm = parts[1].substr(0, 4);
}
// Get the integer part
if (parseInt(integerNum, 10) > 0) {
let zeroCount = 0;
for (let i = 0; i < integerNum.length; i++) {
const n = integerNum.substr(i, 1);
const p = integerNum.length - i - 1;
const q = p / 4;
const m = p % 4;
if (n === '0') {
zeroCount++;
} else {
if (zeroCount > 0) {
chineseStr += cnNums[0];
}
// Zeroing
zeroCount = 0;
chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if (m === 0 && zeroCount < 4) {
chineseStr += cnIntUnits[q];
}
}
chineseStr += cnIntLast;
}
// The fractional part
if (decimaNUm != '') {
for (let i = 0; i < decimaNUm.length; i++) {
const n = decimaNUm.substr(i, 1);
if (n != '0') {
chineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
if (chineseStr === '') {
chineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (decimaNUm === '') {
chineseStr += cnInteger;
}
return chineseStr;
}
Self access required
边栏推荐
- How to choose a technology stack for web applications in 2022
- 深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)
- 如何在 2022 年为 Web 应用程序选择技术堆栈
- [world history] Episode II: Dawn of civilization
- Dialogue: recommended system quick start route and summary of knowledge points
- Why should programmers be softer?
- JS functions
- What are the red lines of open source that should not be trodden on?
- Discriminative v.s.Generative
- Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
猜你喜欢

The best screenshot tool in the world, color absorption tool snipaste

电脑必须打开的设置

Nine parts of speech and nine tenses in English

It's not easy to understand the data consistency of the microservice architecture for the first time after six years as a programmer

JVM uses tools to analyze classic cases of OOM

Settings the PC must be turned on

多台云服务器的 Kubernetes 集群搭建

Installation and removal of MySQL under Windows

Does stream even have application advanced learning? As a programmer, you know what

Shell operator
随机推荐
Let and const commands
Where can the brightness of win7 display screen be adjusted
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
Deconstruction assignment of variables
go---- mgo
作为一名软件测试工程师你认为怎么样才能保证软件质量?
What are the red lines of open source that should not be trodden on?
Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
Stream竟然还有应用进阶学习?作为程序员的你知道吗
Discriminative v.s.Generative
Cool assembly tutorial - concise x86 assembly language tutorial (1)
112页机器学习-数学基础回顾.pptx
Why should programmers be softer?
VGA display of de2-115 FPGA development board
Application of tactile intelligent sharing-rk3568 in financial self-service terminal
Tencent cloud builds a Socks5 multi IP proxy server to realize the perfect building of a game with a single window and a single IP. Tutorial attached tool "suggestions collection"
Logistic Regression VS Linear Regression
Report on Hezhou air32f103cbt6 development board
Preventing overfitting of deep neural networks (mysteries of neural networks Part II)
One time summary: 64 common terms for data analysis!