Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xmcommon

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmcommon - npm Package Compare versions

Comparing version 0.0.18 to 0.0.19

24

example/testcurr.js

@@ -34,5 +34,27 @@

m.push(new CNYCurrency(325.04));
function fff(v) {
m.push(new CNYCurrency(v));
}
fff(0.05);
let jjj = new CNYCurrency(0.1);
jjj.Chinese();
fff(1.20);
fff(1.23);
fff(20.56);
fff(15.00);
fff('56.08');
fff(200.11);
fff(205.12);
fff(5001.09);
fff(67004.00);
fff(200080.07);
fff(0.2);
fff(0.15);
for(let mm of m) {
let c2 = mm.Chinese();
console.log(mm.isErr,mm.value, mm.format(true, true), c2, mm.errMsg);
console.log(mm.isErr, mm.value.toFixed(2).padStart(20, ' '), mm.format(true, false).padStart(20,' '), c2, mm.errMsg);
}

143

lib/cnycurrency.js

@@ -1,2 +0,3 @@

const _ = require("lodash");
// const _ = require("lodash");
let {utils} = require('./utils');
/** 货币精度 */

@@ -13,2 +14,13 @@ const Precision = 100;

//汉字的数字
let cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
//基本单位
let cnIntRadice =['', '拾', '佰', '仟'];
//对应整数部分扩展单位
let cnIntUnits = ['', '万', '亿', '兆'];
//对应小数部分单位
let cnDecUnits = ['角', '分', '毫', '厘'];
//整型完以后的单位
let cnIntLast = '元';
/**

@@ -27,3 +39,3 @@ * 将值转换为数字,

}
if(_.isNumber(paramValue)) {
if(utils.isNumber(paramValue)) {
r.value = paramValue;

@@ -330,3 +342,5 @@ }

get yuan() {
return ~~this.value;
// return ~~this.value;
let n = this.m_IntValue;
return (n - n % Precision)/Precision;
}

@@ -337,3 +351,5 @@ /**

get cent() {
return this.m_IntValue % Precision;
let c = this.m_IntValue % Precision ;
if (c < 0) c = -c;
return c;
}

@@ -351,53 +367,64 @@ /**

* - 增加在元后,角为0的情况增加0,如0.01为零元零角
* @param {string} paramNegative 如果是负数时,前面的前缀
* - 选项prefix:表示自定义人民币前缀,没有默认为“人民币”
* - 选项negative:表示自定义负数前缀,没有默认为“负”
* - 选项zheng:表示自定义整后缀,没有默认为“整”,有些情况可能需要“正”, 使用的时候请注意选项
* @param {{prefix?: string, negative?:string, zheng?: string}} paramOpts 如果是负数时,前面的前缀
* @return {string} 中文大写结果
*/
Chinese(paramNegative = '负') {
//汉字的数字
let cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
//基本单位
let cnIntRadice =['', '拾', '佰', '仟'];
//对应整数部分扩展单位
let cnIntUnits = ['', '万', '亿', '兆'];
//对应小数部分单位
let cnDecUnits = ['角', '分', '毫', '厘'];
//整数金额时后面跟的字符
let cnInteger = '整';
//整型完以后的单位
let cnIntLast = '元';
//最大处理的数字
let maxNum = 999999999999999.9999;
//金额整数部分
let integerNum;
//金额小数部分
let decimalNum;
Chinese(paramOpts = null) {
let stPrefix = '人民币';
let stNegative = '负';
let stZheng = '整';
if (utils.isObject(paramOpts)) {
// 人民币前缀
if (utils.isString(paramOpts.prefix)) {
stPrefix = paramOpts.prefix;
}
// 负数前缀
if (utils.isString(paramOpts.negative)) {
stNegative = paramOpts.negative;
}
// 整或正字
if (utils.isString(paramOpts.zheng)) {
stZheng = paramOpts.zheng;
}
}
//paramPrefix = '人民币', paramNegative = '负') {
//输出的中文金额字符串
let chineseStr = '';
//分离金额后用的数组,预定义
let parts;
// 负数时前缀
let negative = '';
if(this.intValue < 0) {
negative = paramNegative;// '负';
let intValue = this.intValue;
if(intValue < 0) {
negative = stNegative;// '负';
}
let money = Math.abs(this.value);
if (money >= maxNum) {
//超出最大处理数字
return negative + '';
let yuan = this.yuan; // 元
if (yuan < 0) yuan = -yuan;
let cent = this.cent; // 分
//金额整数部分
let integerNum = yuan.toString();
//金额小数部分
let decimalNum = cent.toString().padStart(2, '0');
// if(cent === 0) {
// decimalNum = '';
// }
if (this.intValue === 0) {
// 如果金额为0
chineseStr = [stPrefix, cnNums[0], cnIntLast, stZheng].join('');
return chineseStr;
}
if (money == 0) {
chineseStr = negative + cnNums[0] + cnIntLast + cnInteger;
if (yuan === 0 && cent < 10) {
// 如果只存在分的情况 就是零元零角几分
// chineseStr = [paramPrefix, negative, cnNums[0], cnIntLast, cnNums[0], cnDecUnits[0],cnNums[cent],cnDecUnits[1]].join('');
chineseStr = [stPrefix, negative, cnNums[cent],cnDecUnits[1]].join('');
return chineseStr;
}
//转换为字符串
money = money.toString();
if (money.indexOf('.') == -1) {
integerNum = money;
decimalNum = '';
} else {
parts = money.split('.');
integerNum = parts[0];
decimalNum = parts[1].substr(0, 4);
}
//获取整型部分转换
if (parseInt(integerNum, 10) > 0) {
if (yuan > 0) {
let zeroCount = 0;

@@ -425,25 +452,25 @@ let IntLen = integerNum.length;

chineseStr += cnIntLast;
}else if(yuan === 0 && cent > 0) {
// chineseStr = cnNums[0] + cnIntLast;
}
//小数部分
if (decimalNum != '') {
if (cent > 0) {
let decLen = decimalNum.length;
for (let i = 0; i < decLen; i++) {
let n = decimalNum.substr(i, 1);
if (n != '0') {
if (n !== '0') {
chineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
else {
} else {
if (i === 0) {
chineseStr += cnNums[0];
}
}
}
}
if (chineseStr == '') {
chineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (decimalNum == '') {
chineseStr += cnInteger;
if ((cent % 10) === 0) {
// 当分为0的时候,有整
chineseStr += stZheng;
}
return negative + chineseStr;
return stPrefix + negative + chineseStr;
}

@@ -457,4 +484,4 @@ /**

format(paramUseSymbol = false, paramCNYsplit = false) {
let values = Math.abs(this.value);
values = values.toFixed(2).split('.');
let value = Math.abs(this.value);
let values = value.toFixed(2).split('.');
let yuan = values[0];

@@ -476,3 +503,3 @@ let cent = values[1];

}
return `${strSymbol}${negative}${yuan}.${cent}`;
return [strSymbol, negative, yuan, '.', cent].join('');// `${strSymbol}${negative}${yuan}.${cent}`;
}

@@ -479,0 +506,0 @@

{
"name": "xmcommon",
"version": "0.0.18",
"version": "0.0.19",
"description": "javascript common lib for es6",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -34,3 +34,3 @@ # xmcommon

## 0.0.18
## 0.0.19

@@ -41,2 +41,3 @@ - 2019-06-26

- \* 修复乘除的bug, 对于自乘与自除,返回的是boolean,判断成功失败
- \* 优化人民币大写生成, 主要有小于1元的情况,分为0的时候,要加整

@@ -43,0 +44,0 @@ ## 0.0.17

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc