Java编写钱币转换为大写程序
import java.text.DecimalFormat; public class ChangeMonney { private static final String[] STR_UNIT = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] STR_NUMBER = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"}; public static void main(String[] args) { // TODO Auto-generated method stub double d = 1234567; System.out.print(convert(d)); } //钱币转换 public static String convert(double d) { DecimalFormat dFormat = new DecimalFormat("#0.###"); String Num = dFormat.format(d); if (Num.indexOf(".")!=-1) { String strNum = Num.substring(0,Num.indexOf(".")); if(strNum.length()>12) System.out.println("数字太大了,不能完成转换"); } String point = ""; if(Num.indexOf(".")!=-1) { point ="元"; } else point ="元整"; String result = getInter(Num); return result; } public static String getInter(String Num) { //判断是否含有小数点 if (Num.indexOf(".")!=-1) { Num = Num.substring(0,Num.indexOf(".")); } Num = new StringBuffer(Num).reverse().toString(); StringBuffer temp = new StringBuffer(); for (int i = 0; i < Num.length(); i++) { temp.append(STR_NUMBER[i]); temp.append(STR_UNIT[Num.charAt(i)-48]); } Num = temp.reverse().toString(); Num = Num.replace("零拾", "零"); Num = Num.replace("零佰", "零"); Num = Num.replace("零仟", "零"); Num = Num.replace("零万", "零"); Num = Num.replace("零亿", "零"); return Num; } }
本文出自 “风雨兼程” 博客,请务必保留此出处http://carben.blog.51cto.com/8690350/1643544
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。