From 5fcbc84c463f5d16952fe6a7227c01a9bc9d621b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Wed, 5 Sep 2018 10:29:43 +0800 Subject: [PATCH] Replace your own algorithm with nzh --- package.json | 1 + src/utils/utils.js | 34 ++-------------------------------- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index eddd6448..ff8343ce 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "memoize-one": "^4.0.0", "moment": "^2.22.2", "numeral": "^2.0.6", + "nzh": "^1.0.3", "omit.js": "^1.0.0", "path-to-regexp": "^2.4.0", "prop-types": "^15.5.10", diff --git a/src/utils/utils.js b/src/utils/utils.js index fdb32a7f..3c795c22 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -1,5 +1,6 @@ import moment from 'moment'; import React from 'react'; +import nzh from 'nzh/cn'; import { parse, stringify } from 'qs'; export function fixedZero(val) { @@ -69,39 +70,8 @@ export function getPlainNode(nodeList, parentPath = '') { return arr; } -function accMul(arg1, arg2) { - let m = 0; - const s1 = arg1.toString(); - const s2 = arg2.toString(); - m += s1.split('.').length > 1 ? s1.split('.')[1].length : 0; - m += s2.split('.').length > 1 ? s2.split('.')[1].length : 0; - return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / 10 ** m; -} - export function digitUppercase(n) { - const fraction = ['角', '分']; - const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; - const unit = [['元', '万', '亿'], ['', '拾', '佰', '仟', '万']]; - let num = Math.abs(n); - let s = ''; - fraction.forEach((item, index) => { - s += (digit[Math.floor(accMul(num, 10 * 10 ** index)) % 10] + item).replace(/零./, ''); - }); - s = s || '整'; - num = Math.floor(num); - for (let i = 0; i < unit[0].length && num > 0; i += 1) { - let p = ''; - for (let j = 0; j < unit[1].length && num > 0; j += 1) { - p = digit[num % 10] + unit[1][j] + p; - num = Math.floor(num / 10); - } - s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s; - } - - return s - .replace(/(零.)*零元/, '元') - .replace(/(零.)+/g, '零') - .replace(/^整$/, '零元整'); + return nzh.toMoney(n); } function getRelation(str1, str2) { -- GitLab