JAVA 将数字字符串转换成中文形式

本文介绍了一个Java程序,该程序能够将输入的小数转换成对应的中文金额表述,适用于财务系统中金额显示的需求。程序首先处理输入字符串,去除前导零,并针对整数和小数部分分别进行转换。
public class MainClass3 {

/**
* @param args
* @deprecated 完成小数的中文转换
* @deprecated 核心工式:unit[(len - i - 1) % 8]
* @deprecated
* 1-0-1=0%8 亿
* 2-0-1=1%8 拾
* 3-0-1=2%8 佰
* 4-0-1=3%8 仟
* 5-0-1=4%8 万
* 6-0-1=5%8 拾
* 7-0-1=6%8 佰
* 8-0-1=7%8 仟
* 9-0-1=9%8 亿
*/
public static void main(String[] args) {

String temp0 = "987.65";// "009081";
String temp = temp0;

if (temp0.startsWith("0")) {
temp = String.valueOf(Integer.parseInt(temp0));
// System.out.println(Integer.parseInt(temp));
}
// int itemp = Integer.valueOf(temp);
// System.out.println("String --> Integer"+itemp);
StringBuffer retStrBuf = new StringBuffer();// 用来拼接数字串
int dot = 0; // 小数点位置
int ivalue = 0;// 保存每一个位上的数
int len = 0;// 保存数字的长度
for (int i = 0; i < temp.length(); i++) {
System.out.print(temp.substring(i, i + 1) + " ");
}
System.out.println();
String num[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String unit[] = { "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };

len = temp.length();
if (temp == null || "".equals(temp) || temp.length() <= 0) {
System.out.println("Input is null");
} else {
//(==-1)说明
dot = temp.indexOf(".");
if(dot == -1){
int i = 0;
// len-1这个注意,i在前面初始化为0
for (i = 0; i < len - 1; i++) {
ivalue = Integer.parseInt(temp.substring(i, i + 1));
retStrBuf.append(num[ivalue] + (unit[(len - i - 1) % 8]));
// System.out.print(num[ivalue]+(unit[(len - i - 1) % 8]));
}
// 单独取最后一位数
ivalue = Integer.parseInt(String.valueOf(temp.substring(i, i + 1)));
// 最后一位为零,将不作处理
if (ivalue != 0) {
retStrBuf.append(num[ivalue]);
}
retStrBuf.append("元整");
System.out.println(retStrBuf.toString());
// System.out.println(","+num[ivalue]);
}else{
String tmpStr1 = temp.substring(0, dot);
len = tmpStr1.length();
int i = 0;
// len-1这个注意,i在前面初始化为0
for (i = 0; i < len - 1; i++) {
ivalue = Integer.parseInt(temp.substring(i, i + 1));
retStrBuf.append(num[ivalue] + (unit[(len - i - 1) % 8]));
// System.out.print(num[ivalue]+(unit[(len - i - 1) % 8]));
}
// 单独取最后一位数
ivalue = Integer.parseInt(String.valueOf(temp.substring(i, i + 1)));
// 最后一位为零,将不作处理
if (ivalue != 0) {
retStrBuf.append(num[ivalue]);
}

//跟整数处理不一样的部分
if (dot < temp.length() - 1) {
retStrBuf.append("点");
//从小数点后一位开始取
String tmpStr2 = temp.substring(dot + 1);
len = tmpStr2.length();
//这里不需要len-1
for (i = 0; i < len; i++) {
ivalue = Integer.parseInt(tmpStr2.substring(i, i + 1));
retStrBuf.append(num[ivalue]);
}
}

retStrBuf.append("元整");

System.out.println(retStrBuf.toString());
}
}

}
}
### 将 Java 中的 String 换为 Int 的方法 在 Java 编程语言中,可以使用多种方式将 `String` 类型的数据换为 `int` 数据类型。以下是几种常见的实现方法及其注意事项。 #### 方法一:使用 `Integer.parseInt()` 函数 这是最常用的方法之一,用于将字符串解析为整数值。如果输入的字符串不是有效的整数形式,则会抛出 `NumberFormatException` 异常[^2]。 ```java public class Main { public static void main(String[] args) { String str = "123"; try { int num = Integer.parseInt(str); System.out.println("换后的整数是: " + num); } catch (NumberFormatException e) { System.out.println("错误: 字符串无法换为整数"); } } } ``` #### 方法二:使用 `Integer.valueOf()` 方法 此方法不仅能够完成字符串到整数的换,还会返回一个 `Integer` 对象而不是基本数据类型的 `int` 值。需要注意的是,在某些情况下可能会触发自动拆箱操作[^1]。 ```java public class Main { public static void main(String[] args) { String str = "456"; try { Integer integerObject = Integer.valueOf(str); // 返回 Integer 对象 int primitiveIntValue = integerObject.intValue(); // 获取原始值 System.out.println(primitiveIntValue); } catch (NumberFormatException ex) { System.err.println(ex.getMessage()); } } } ``` #### 方法三:利用自定义封装函数处理潜在异常 当面对可能含有非法字符或者不确定格式的输入源时,采用更稳健的方式显得尤为重要。下面展示了一个基于可选容器类 (`Optional`) 实现的安全包装器来应对这种情况[^3]: ```java import java.util.Optional; public class SafeConversionExample { private static Optional<Integer> safeParseInteger(String inputStr){ try{ return Optional.of(Integer.valueOf(inputStr)); }catch(NumberFormatException nfe){ return Optional.empty(); } } public static void main(String []args){ String testInput="789b"; Optional<Integer> result=safeParseInteger(testInput); if(result.isPresent()){ System.out.println("成功换:"+result.get()); }else{ System.out.println("未能正确换给定字符串!"); } } } ``` 以上三种技术各有优劣,开发者应根据实际应用场景选择最适合的技术方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值