JAVA阿拉伯数字金额插入单位(例如:123456789.123转换成 1亿2千3百4十5万6千7百8十9元1角2分)

package snippet;


import java.util.Scanner;
public class Money {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Please input the number: ");
String word = sc.next();
int length = word.length();
int point = length - 1;//记录小数点位置
char[] chars = word.toCharArray();
for (int i = 0; i < length; i++) {
if (".".equals(chars[i] + "")) 
point = i;
}

String mony = "";
int l = 0;
for (int i = 0; i < length; i++) {
if (point != length - 1)
l = 1;
if (!".".equals(chars[i] + "")) {


if (point - i > 8 + l) {
mony = mony + chars[i];
}
if (point - i == 8 + l) {
mony = mony + chars[i] + "亿";
}
if (point - i == 7 + l) {
mony = mony + chars[i] + "千";
}
if (point - i == 6 + l) {
mony = mony + chars[i] + "百";
}
if (point - i == 5 + l) {
mony = mony + chars[i] + "十";
}
if (point - i == 4 + l) {
mony = mony + chars[i] + "万";
}
if (point - i == 3 + l) {
mony = mony + chars[i] + "千";
}
if (point - i == 2 + l) {
mony = mony + chars[i] + "百";
}
if (point - i == 1 + l) {
mony = mony + chars[i] + "十";
}
if (point - i == 0 + l) {
mony = mony + chars[i] + "元";
}
if (point - i == -1) {
mony = mony + chars[i] + "角";
}
if (point - i == -2) {
mony = mony + chars[i] + "分";
}
}
}
System.out.println(mony);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值