将多位数转换成中文显示

 

 

 

pipe.ts:

 

import { Pipe, PipeTransform } from '@angular/core';

 

/**

* @title:将多位数转换成汉字的算法

* @author:liuyu

* @date:2018/04/28

* @rule:1、十以内直接转换;2、万及亿以上的考虑连续0的问题;

* 默认返回值为个位

*/

@Pipe({name: 'parseNumChinese'})

export class ParseNumChinesePipe implements PipeTransform {

private map_obj: any = {};

private units = [];

private newArray: any; //整合后的新数组

private isZero: boolean; //判断上一个添加的是否为零

private resValue: string; //管道的返回值

constructor() {

this.map_obj = {

"0": "零",

"1": "一",

"2": "二",

"3": "三",

"4": "四",

"5": "五",

"6": "六",

"7": "七",

"8": "八",

"9": "九"

};

this.units = ["","十","百","千","万","十","百","千","亿","十","百","千","万"];

this.newArray = [];

this.isZero = false;

this.resValue = '';

}

transform(s: any, type: string):string {

s = s * 1;

if (!s || s <= 0) {

return '';

}

if ( type === 'arabian') {

return s;

} else if( type === 'chinese'){

return this.isChiese(s);

} else {

return '';

}

}

 

//判断当前index是否在万、亿,4和8的倍数

isMultiple(i, val) {

let y = i % 4;

let x = i % 8;

if( this.isZero ) { //前一位为零

if ( y === 0 && x !== 0) { //仅是4的倍数取万

return "万";

} else if ( i>8 && x === 0 ) { //仅是8的倍数取亿

return "亿";

} else { //不正常情况返回空

return "";

}

} else { //第一次为零进入

this.isZero = true;

if ( y === 0 && x !== 0) { //仅是4的倍数取万

return "万";

} else if ( i>8 && x === 0 ) { //仅是8的倍数取亿

return "亿";

} else { //不正常情况返回空

return val;

}

}

}

//当type == Chinese时,fn

isChiese(num) {

const os = num + '';

let os_arr = os.split('').reverse(); // 字符串分割后数组反转

this.newArray = [];

this.resValue = '';

for (let index = 0, j = os_arr.length; index<j; index++) {

if (os_arr[index] === '0') {

let oval = this.isMultiple(index, this.map_obj[os_arr[index]]);

this.newArray.push(oval);

} else {

os_arr[index] = this.map_obj[os_arr[index]] + this.units[index];

this.newArray.push(os_arr[index]);

this.isZero = false;

}

}

if (this.newArray[0] == "零") {

this.newArray[0] = '';

}

for (let ostr of this.newArray.reverse()) {

this.resValue += ostr;

}

return this.resValue ? this.resValue : '';

}

}

 

 

pipe.ts:

 

import { Pipe, PipeTransform } from '@angular/core';

 

/**

* @title:将多位数转换成汉字的算法

* @author:liuyu

* @date:2018/04/28

* @rule:1、十以内直接转换;2、万及亿以上的考虑连续0的问题;

* 默认返回值为个位

*/

@Pipe({name: 'parseNumChinese'})

export class ParseNumChinesePipe implements PipeTransform {

private map_obj: any = {};

private units = [];

private newArray: any; //整合后的新数组

private isZero: boolean; //判断上一个添加的是否为零

private resValue: string; //管道的返回值

constructor() {

this.map_obj = {

"0": "零",

"1": "一",

"2": "二",

"3": "三",

"4": "四",

"5": "五",

"6": "六",

"7": "七",

"8": "八",

"9": "九"

};

this.units = ["","十","百","千","万","十","百","千","亿","十","百","千","万"];

this.newArray = [];

this.isZero = false;

this.resValue = '';

}

transform(s: any, type: string):string {

s = s * 1;

if (!s || s <= 0) {

return '';

}

if ( type === 'arabian') {

return s;

} else if( type === 'chinese'){

return this.isChiese(s);

} else {

return '';

}

}

 

//判断当前index是否在万、亿,4和8的倍数

isMultiple(i, val) {

let y = i % 4;

let x = i % 8;

if( this.isZero ) { //前一位为零

if ( y === 0 && x !== 0) { //仅是4的倍数取万

return "万";

} else if ( i>8 && x === 0 ) { //仅是8的倍数取亿

return "亿";

} else { //不正常情况返回空

return "";

}

} else { //第一次为零进入

this.isZero = true;

if ( y === 0 && x !== 0) { //仅是4的倍数取万

return "万";

} else if ( i>8 && x === 0 ) { //仅是8的倍数取亿

return "亿";

} else { //不正常情况返回空

return val;

}

}

}

//当type == Chinese时,fn

isChiese(num) {

const os = num + '';

let os_arr = os.split('').reverse(); // 字符串分割后数组反转

this.newArray = [];

this.resValue = '';

for (let index = 0, j = os_arr.length; index<j; index++) {

if (os_arr[index] === '0') {

let oval = this.isMultiple(index, this.map_obj[os_arr[index]]);

this.newArray.push(oval);

} else {

os_arr[index] = this.map_obj[os_arr[index]] + this.units[index];

this.newArray.push(os_arr[index]);

this.isZero = false;

}

}

if (this.newArray[0] == "零") {

this.newArray[0] = '';

}

for (let ostr of this.newArray.reverse()) {

this.resValue += ostr;

}

return this.resValue ? this.resValue : '';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值