java将输入的数字金额转换为中文大写金额

前言

将输入的数字字符串中的每一个数字字符转换为对应的中文大写,例如12345转换为壹贰叁肆伍,再将此中文大写和对应的单位进行字符串的拼接并一起输出

输出结果为:零佰零拾壹万贰仟叁佰肆拾伍元


一、具体代码

package com.ithm.stringdemo;
import java.util.Scanner;

public class Demo1 {
    //获取每一个数字对应的中文大写
    public static String getCapitalNumber(int number){
        String []capitalnumber={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        return capitalnumber[number];
    }
    public static void main(String[] args) {
        //数组里面放入输入的金额的每一位数字
        int []sz=new int[7];
        Scanner sc=new Scanner(System.in);
        int money;
       while (true){
           System.out.print("请输入任意金额:");
           money=sc.nextInt();
           if(money>=0 ||money<=9999999) {
           break;
       }else {
               System.out.println("金额无效,请重新输入");
           }
       }
       String capmoney="";
       int i=0;
       while (true){
           int ge=money%10;
           sz[i++]=ge;
           money=money/10;
           if(money==0) break;
       }
       //获取大写数字的字符串
        String capitalnumber="";
        for (int j = 0; j < sz.length; j++) {
            capitalnumber=getCapitalNumber(sz[j]);
            capmoney=capitalnumber+capmoney;
        }
        //定义一个字符串数组存放单位
        String []arr={"佰","拾","万","仟","佰","拾","元"};
        //将大写数字和单位数组进行字符串的拼接
        String result="";
        for (int k = 0; k < capmoney.length(); k++) {
            char c=capmoney.charAt(k);
            result=result+c+arr[k];
        }
        System.out.println(result);
    }
}

二、运行结果

 

 

 

 

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以回答这个问题。以下是将数字金额转换中文大写金额JavaScript代码: ```javascript function convertToChineseNumeral(amount) { const chineseNumeral = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; const chineseUnit = ["", "拾", "佰", "仟"]; const chineseBigUnit = ["", "萬", "亿", "兆"]; let num = parseFloat(amount); let chineseAmount = ""; if (isNaN(num)) { return ""; } let isNegative = num < 0; if (isNegative) { num = -num; } let fraction = Math.round((num - Math.floor(num)) * 100); if (fraction == 0) { chineseAmount += "整"; } else { chineseAmount += chineseNumeral[Math.floor(fraction / 10)] + "角"; chineseAmount += chineseNumeral[fraction % 10] + "分"; } num = Math.floor(num); let unitIndex = 0; while (num > 0) { let currentUnit = ""; for (let i = 0; i < 4 && num > 0; i++) { let currentNumeral = num % 10; if (currentNumeral > 0) { currentUnit = chineseUnit[i] + currentUnit; currentUnit = chineseNumeral[currentNumeral] + currentUnit; } else if (currentUnit != "") { currentUnit = chineseNumeral[currentNumeral] + currentUnit; } num = Math.floor(num / 10); } if (currentUnit != "") { currentUnit += chineseBigUnit[unitIndex]; } chineseAmount = currentUnit + chineseAmount; unitIndex++; } if (chineseAmount == "") { chineseAmount = chineseNumeral[0]; } if (isNegative) { chineseAmount = "负" + chineseAmount; } return chineseAmount; } console.log(convertToChineseNumeral(123456789.01)); // 壹亿贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元零壹分 ``` 希望这个代码能帮到你。如果你有其它问题,欢迎继续询问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值