把钱币大写 转为 数字的Java代码
发布时间:2020-12-14 23:51:27 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 把钱币大写 转为 数字的Java代码 import java.text.DecimalFormat;public class Mm { /** * 转亿以下的 * @param str * @return */ public static dou
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
把钱币大写 转为 数字的Java代码
import java.text.DecimalFormat; public class Mm { /** * 转亿以下的 * @param str * @return */ public static double b2s(String str){ str=str.replaceAll("零",""); double sum=0; int index=str.indexOf("亿"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+w(temp)*100000000; } index=str.indexOf("万"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+w(temp)*10000; } index=str.indexOf("元"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+w(temp); } index=str.indexOf("角"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+str2num(temp)*0.1; } index=str.indexOf("分"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+str2num(temp)*0.01; } return sum; } /** * 大写转小写 * @param str * @return */ public static int str2num(String str){ String[] daxie={"壹","贰","叁","肆","伍","陆","柒","捌","玖"}; int[] num={1,2,3,4,5,6,7,8,9}; for (int i = 0; i < daxie.length; i++) { if ( str.equals( daxie[i] ) ) { return num[i]; } } return 0; } /** * @see 转万以下的 * @param str * @return */ public static double w(String str){ double sum=0; int index=str.indexOf("仟"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+str2num(temp)*1000; } index=str.indexOf("佰"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+str2num(temp)*100; } index=str.indexOf("拾"); if (index>0){ String temp=str.substring(0,index ); str=str.substring(index+1); sum=sum+str2num(temp)*10; } if (str.length()>0) { sum=sum+str2num(str) ; } return sum; } public static void main(String[] args) { DecimalFormat df=new DecimalFormat(); df.setMinimumFractionDigits(2); df.setMaximumFractionDigits(2); System.out.println(df.format(b2s("壹仟万零伍佰壹拾玖元捌角陆分") )); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |