def wan(ip): #万以内大写转换 #ip为要转换的数字 it=['拾','佰','仟'] if set(ip)=={'零'}: return '#' elif ip[-1]=='零' and ip[-2]!='零': ip[-1]='拾' elif ip[-1]=='零' and ip[-2]=='零' and ip[-3]!='零': ip.pop(-1) ip[-1]='佰' if ip[0]=='零' and ip[1]=='零': ip.pop(0) elif ip[-1]=='零' and ip[-2]=='零' and ip[-3]=='零' and ip[-4]!='零': ip.pop(-1) ip.pop(-1) ip[-1]='仟' for j in range(1,len(ip)): ip.insert(-j*2+1, it[(j-1)%3]) op=''.join(ip) import re op=re.sub(r'(零.)零.','',op) #正则表达式处理连续零 op=re.sub(r'(零[拾佰仟萬亿])','零',op) rt= list(op) if rt[-1]=='拾' : rt.pop(-1) return ''.join(rt) def thousandFormat(ip): #千分格式化 #ip为要转换的数字 if not isinstance(ip, str): ip=str(ip) list_str=list(ip) #get input num list for i in range(1,int(len(list_str)/2)): list_str.insert(-i*3-i+1,',') #insert ',' if list_str[0]==',': list_str.pop(0) return ''.join(list_str) def printChinese(ip): #ip为要转换的数字 if not isinstance(ip, str): ip=str(ip) list_str=list(ip) #get input num list out_put=[] char_map={'0':'零','1':'壹','2':'貳','3':'叁','4':'肆','5':'伍','6':'陆','7':'柒','8':'捌','9':'玖'} it=['萬','亿'] for i in list_str: out_put.append(char_map[i]) #得到中文大写 ot='' for i in range(1,int(len(list_str)/4)+1): if i==1: ot=wan(out_put[-4:len(out_put)]) else: ot=wan(out_put[-i*4:-i*4+4])+it[i%2]+ot if len(list_str)%4!=0: ot=wan(out_put[0:len(list_str)%4])+it[(int(len(list_str)/4)+1)%2]+ot return ot input_=input() print(thousandFormat(input_),printChinese(input_))
const regexp = /.{3}/g const group = /.{4}/g const str1 = ['零','壹','貳','叁','肆','伍','陆','柒','捌','玖'] const str2 = ['','萬','亿','兆'] const str3 = ['','拾','佰','仟'] const tagFun = (arr=[],groupNum)=>{ let firstFlat = true array = Array.from(arr).reverse() return sub = array.reduce((pre,value,index)=>{ if(value === ' '){ return pre }else{ const tmp = `${str1[Number(value)]}${value === '0'?'':str3[index]}${firstFlat&&value!=='0' ? str2[groupNum]: ''}${pre}` if(value!=='0') firstFlat = false return tmp } },'') } while(line=readline()){ const format = line.padStart(line.length+3 - (line.length%3),' ').match(regexp).join(',').trim() const numbers = line.split('').reverse() const groups = line.padStart(line.length+4 - (line.length%4),' ').match(group).reverse() const cny = groups.reduce((pre,value,index)=>{ return tagFun(value,index)+pre },'') console.log(format,cny.length === 1 ? cny :cny.replace(/零+/,'零').replace(/零$/,'')) }
var money = readline() function fun1 (money) { // 将金额逢3位用 , 隔开 var str = money.toString() var arr = [] for (var i = 0; i < str.length; i++) { arr.unshift(str[str.length - 1 - i]) if ((i + 1) % 3 === 0 && (i + 1) !==str.length) { arr.unshift(',') } } str = arr.join('') return str } function foo (str) { // 将4位数格式化为中文 var temp = '' for (var i= 0; i < str.length; i++) { switch (str[i]) { case '0': temp += '零' break case '1': temp += '壹' break case '2': temp += '贰' break case '3': temp += '叁' break case '4': temp += '肆' break case '5': temp += '伍' break case '6': temp += '陆' break case '7': temp += '柒' break case '8': temp += '捌' break case '9': temp += '玖' break } if (str[i] !== '0') { // if (i === 0) { temp += '仟' } else if (i === 1) { temp += '佰' } else if (i === 2) { temp += '拾' } } } // 1个‘零’ 0123 1023 1203 1230 去除一个或多个‘零’ temp = temp.replace(/零+$/g, '') // 2个‘零’ 0012 0102 0120 1002 1020 1200 将多个‘零’替换为一个‘零’ temp = temp.replace(/零{2,}/g, '零') // 3个‘零’ 0001 0010 0100 1000 // 4个‘零’ 0000 if (temp === '') { temp = '零' } return temp } function fun2 (money) { // 将金额逢四位切割,不足补位 var str = money.toString() var res = '' var tempArr = ['萬億', '億', '萬', ''] // 上限 9999 9999 9999 9999 // 补0 if (str.length % 4 !== 0) { var addZoreCount = 4 - (str.length % 4) for (var i = 0; i < addZoreCount; i++) { str = '0' + str } } // 切割 var tempStr = '' var arr = [] for (var i = 0; i < str.length; i++) { tempStr = str[str.length - 1 - i] + tempStr if ((i + 1) % 4 === 0) { arr.unshift(tempStr) tempStr = '' } } // 转中文 tempArr.splice(0, 4 - arr.length) for (var i = 0; i < arr.length; i++) { if (arr[i] !== '0000') { res = res + foo(arr[i]) + tempArr[i] } } res = res.replace(/^零+|零$/g, '') //去除头尾的‘零’ return res } print(fun1(money), fun2(money))
var s = '1400398'
function translate(s) {
var num = ['', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
var digit = ['', '萬', '亿']
var digitWithinThousand = ['', '拾', '佰', '仟']
var s1 = s.replace(/(?!^)(?=(\d{3})+$)/g, ',')
var strArr = []
for (let i = s.length; i > 0; i = i - 4) {
if (i >= 4) {
strArr.unshift(s.slice(i - 4, i))
} else {
strArr.unshift(s.slice(0, i))
}
}
function strToWordWithinThousand(str) {
str = str.replace(/^0+/, '')
var ret = []
for (let i = str.length - 1; i >= 0; i--) {
var n = parseInt(str[i], 10), s
if (n !== 0) {
s = num[n] + digitWithinThousand[str.length - 1 - i]
} else {
s = '零'
}
ret.push(s)
}
return ret.reduceRight((p, c) => p + c).replace(/零+/g, '零').replace(/零$/, '')
}
var wordArr = strArr.map(e => strToWordWithinThousand(e))
var word = ''
for (let i = 0; i < wordArr.length; i++) {
var dig = digit.slice(0, wordArr.length)
var s = strArr[i].startsWith('0') ?
'零' + wordArr[i] + dig[wordArr.length - 1 - i] :
wordArr[i] + dig[wordArr.length - 1 - i]
word = word + s
}
return s1 + ' ' + word
}
console.log(translate(s))