Java数字⼩写转换为⼤写
闲来⽆事,在⽹上搜索了⼀些⾯试的题⽬,开始练习下,下列代码的作⽤是把数字转换为⼤写的,主要是⼈民币的写法,暂时没有考虑⼩数位。
package st01;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import Matcher;
import Pattern;
public class DigitalLowerToUpper {
private static final Map<String, String> digitDict = new HashMap<String, String>();
static {
digitDict.put("0", "零");
digitDict.put("1", "壹");
digitDict.put("2", "贰");
digitDict.put("3", "叁");
digitDict.put("4", "肆");
digitDict.put("5", "伍");
digitDict.put("6", "陆");
digitDict.put("7", "柒");
digitDict.put("8", "捌");
digitDict.put("9", "玖");
digitDict.put("10", "拾");
}
/**
* 转换
*
* @param obj
* @return
*/
public static String upper(String obj) {
if (obj.length() > 17) {
return "Can not process it!";
}
if (obj.length() <= 17 && obj.length() > 16) {
StringBuffer sb = new StringBuffer();
BigDecimal db10000 = new BigDecimal("10000000000000000");
BigDecimal bd = new BigDecimal(obj);
BigDecimal yushu = bd.remainder(db10000);
BigDecimal shang = bd.divideToIntegralValue(db10000);
if (String().equals("0")) {
return sb.append(String())).append("亿")
.append("亿").toString();
}
if (String().length() < 12) {
if (obj.length() <= 16 && obj.length() > 12) {
StringBuffer sb = new StringBuffer();
BigDecimal db10000 = new BigDecimal("1000000000000");
BigDecimal bd = new BigDecimal(obj);
BigDecimal yushu = bd.remainder(db10000);
BigDecimal shang = bd.divideToIntegralValue(db10000);
if (String().equals("0")) {
return sb.append(String())).append("万")
.append("亿").toString();
}
if (String().length() < 8) {
return sb.append(String())).append("万")
.append("亿").append(String())).toString();
}
return sb.append(String())).append("万")
.append(String())).toString();
}
if (obj.length() <= 12 && obj.length() > 8) {
StringBuffer sb = new StringBuffer();
BigDecimal db10000 = new BigDecimal("100000000");
BigDecimal bd = new BigDecimal(obj);
BigDecimal yushu = bd.remainder(db10000);
BigDecimal shang = bd.divideToIntegralValue(db10000);
if (String().length() == 1 && !String().equals("0")) { return sb.append(String())).append("亿")
.append("零").append(String())).toString();
}
return sb.append(String())).append("亿")
.append(String())).toString();
}
if (obj.length() <= 8 && obj.length() > 4) {
StringBuffer sb = new StringBuffer();
BigDecimal db10000 = new BigDecimal("10000");
BigDecimal bd = new BigDecimal(obj);
BigDecimal yushu = bd.remainder(db10000);
BigDecimal shang = bd.divideToIntegralValue(db10000);
if (String().length() == 1 && !String().equals("0")) {
if (obj.length() <= 4) {
StringBuffer sb = new StringBuffer();
return sb.append(getYuanzi(obj)).toString(); }
String result = "";
return result;
}
/**
* 格式化字符串中的零
*
* @param obj
* @return
*/
public static String format(String obj) {
if (!obj.startsWith("0")) {
return obj;
}
Pattern p = Patternpile("^0*");
Matcher m = p.matcher(obj);
String result = null;
if (m.find()) {
result = obj.substring(m.start() + 1);
}
if (result.startsWith("0")) {
return format(result);
}
return result;
}
/**
* 递归调⽤的原⼦⽅法,以万位进位
*
* @param obj
* @return
*/
bigdecimal格式化两位小数public static String getYuanzi(String obj) {
String format = format(obj);
StringBuffer sb = new StringBuffer();
if (format.length() == 0) {
String();
}
if (obj.equals("0") && obj.length() == 1) {
if (format.length() != obj.length()) {
sb.append("零");
}
if (format.length() == 4) {
String qian = format.substring(0, 1);
String shengxia = format.substring(1);
return sb.(qian)).append("千")
.append(getYuanzi(shengxia)).toString();
}
if (format.length() == 3) {
String qian = format.substring(0, 1);
String shengxia = format.substring(1);
return sb.(qian)).append("佰")
.append(getYuanzi(shengxia)).toString();
}
if (format.length() == 2) {
String qian = format.substring(0, 1);
String shengxia = format.substring(1);
return sb.(qian)).append("拾")
.append(getYuanzi(shengxia)).toString();
}
if (format.length() == 1) {
String qian = format.substring(0, 1);
if (qian.equals("0")) {
String();
}
String shengxia = format.substring(1);
return sb.(qian)).append(getYuanzi(shengxia))
.toString();
}
String();
}
public static void main(String[] args) {
/
/ System.out.println(upper("45678123410000010"));
// System.out.println(upper("40000100000100001"));
//System.out.println(upper("40000000000000000"));
//System.out.println(upper("4000000000000"));
System.out.println(upper("4000000000001"));
System.out.println(upper("4000001000101"));
//System.out.println(upper("400000000"));
//System.out.println(upper("400000001"));
//System.out.println(upper("400010001"));
//System.out.println(upper("411110001"));
//System.out.println(upper("40000"));
/
/System.out.println(upper("40001"));
//System.out.println(upper("40101"));
}
}
本⼈在main⽅法中列举出了⼀些测试例⼦,⽬前为⽌还么发现问题,哪位⾼⼿如果发现了,欢迎⼤家给我指出来,谢谢。另外,次⽅法只⽀持17位⼀下的转换。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论