知识大全 Java转换大写金额的代码

Posted

篇首语:人间有三愿,一愿识尽世间好人,二愿读尽世间好书,三愿看尽世间好风光。人间最美四月天,祝你三愿皆有所获。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java转换大写金额的代码相关的知识,希望对你有一定的参考价值。

Java转换大写金额的代码  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  此代码根据网上搜集到的部分代码进行修改 使得输出准确完整

  package PrintOperate;

  import java text NumberFormat;    import java util HashMap;

  public class SimpleMoneyFormat   

  public static final String EMPTY = ;        public static final String ZERO = 零 ;        public static final String ONE = 壹 ;        public static final String O = 贰 ;        public static final String THREE = 三 ;        public static final String FOUR = 肆 ;        public static final String FIVE = 伍 ;        public static final String SIX = 陆 ;        public static final String SEVEN = 柒 ;        public static final String EIGHT = 捌 ;        public static final String NINE = 玖 ;        public static final String TEN = 拾 ;        public static final String HUNDRED = 佰 ;        public static final String THOUSAND = 仟 ;        public static final String TEN_THOUSAND = 万 ;        public static final String HUNDRED_MILLION = 亿 ;        public static final String YUAN = 元 ;        public static final String JIAO = 角 ;        public static final String FEN = 分 ;        public static final String DOT = ;        private static SimpleMoneyFormat formatter = null;        private HashMap chineseNumberMap = new HashMap();        private HashMap chineseMoneyPattern = new HashMap();        private NumberFormat numberFormat = NumberFormat getInstance();

  private SimpleMoneyFormat()                    numberFormat setMaximumFractionDigits( );            numberFormat setMinimumFractionDigits( );            numberFormat setGroupingUsed(false);

  chineseNumberMap put( ZERO);            chineseNumberMap put( ONE);            chineseNumberMap put( O);            chineseNumberMap put( THREE);            chineseNumberMap put( FOUR);            chineseNumberMap put( FIVE);            chineseNumberMap put( SIX);            chineseNumberMap put( SEVEN);            chineseNumberMap put( EIGHT);            chineseNumberMap put( NINE);            chineseNumberMap put(DOT DOT);

  chineseMoneyPattern put( TEN);            chineseMoneyPattern put( HUNDRED);            chineseMoneyPattern put( THOUSAND);            chineseMoneyPattern put( TEN_THOUSAND);            chineseMoneyPattern put( TEN);            chineseMoneyPattern put( HUNDRED);            chineseMoneyPattern put( THOUSAND);            chineseMoneyPattern put( HUNDRED_MILLION);       

  public static SimpleMoneyFormat getInstance()                    if (formatter == null)                            formatter = new SimpleMoneyFormat();                        return formatter;       

  public String format(String moneyStr)                    checkPrecision(moneyStr);            String result;            result = convertToChineseNumber(moneyStr);            result = addUnitsToChineseMoneyString(result);            return result;       

  public String format(double moneyDouble)                    return format(numberFormat format(moneyDouble));       

  public String format(int moneyInt)                    return format(numberFormat format(moneyInt));       

  public String format(long moneyLong)                    return format(numberFormat format(moneyLong));       

  public String format(Number moneyNum)                    return format(numberFormat format(moneyNum));       

  private String convertToChineseNumber(String moneyStr)                    String result;            StringBuffer cMoneyStringBuffer = new StringBuffer();            for (int i = ; i < moneyStr length(); i++)                            cMoneyStringBuffer append(chineseNumberMap get(moneyStr substring(i i + )));                        //拾佰仟万亿等都是汉字里面才有的单位 加上它们            int indexOfDot = cMoneyStringBuffer indexOf(DOT);            int moneyPatternCursor = ;            for (int i = indexOfDot ; i > ; i )                            cMoneyStringBuffer insert(i chineseMoneyPattern get(EMPTY + moneyPatternCursor));                moneyPatternCursor = moneyPatternCursor == ? : moneyPatternCursor + ;           

  String fractionPart = cMoneyStringBuffer substring(cMoneyStringBuffer indexOf( ));            cMoneyStringBuffer delete(cMoneyStringBuffer indexOf( ) cMoneyStringBuffer length());            while (cMoneyStringBuffer indexOf( 零拾 ) != )                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零拾 ) cMoneyStringBuffer indexOf( 零拾 ) + ZERO);                        while (cMoneyStringBuffer indexOf( 零佰 ) != )                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零佰 ) cMoneyStringBuffer indexOf( 零佰 ) + ZERO);                        while (cMoneyStringBuffer indexOf( 零仟 ) != )                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零仟 ) cMoneyStringBuffer indexOf( 零仟 ) + ZERO);                        while (cMoneyStringBuffer indexOf( 零万 ) != )                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零万 ) cMoneyStringBuffer indexOf( 零万 ) + TEN_THOUSAND);                        while (cMoneyStringBuffer indexOf( 零亿 ) != )                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零亿 ) cMoneyStringBuffer indexOf( 零亿 ) + HUNDRED_MILLION);                        while (cMoneyStringBuffer indexOf( 零零 ) != )                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零零 ) cMoneyStringBuffer indexOf( 零零 ) + ZERO);                        if (cMoneyStringBuffer lastIndexOf(ZERO) == cMoneyStringBuffer length() )                            cMoneyStringBuffer delete(cMoneyStringBuffer length() cMoneyStringBuffer length());                        cMoneyStringBuffer append(fractionPart);

  result = cMoneyStringBuffer toString();            return result;       

  private String addUnitsToChineseMoneyString(String moneyStr)                    String result;            StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);            int indexOfDot = cMoneyStringBuffer indexOf(DOT);            cMoneyStringBuffer replace(indexOfDot indexOfDot + YUAN);            cMoneyStringBuffer insert(cMoneyStringBuffer length() JIAO);            cMoneyStringBuffer insert(cMoneyStringBuffer length() FEN);            if (cMoneyStringBuffer indexOf( 零角零分 ) != )//没有零头 加整                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零角零分 ) cMoneyStringBuffer length() 整 );                        else if (cMoneyStringBuffer indexOf( 零分 ) != )//没有零分 不输出零分 也不加整                            cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零分 ) cMoneyStringBuffer length() );                        else if (cMoneyStringBuffer indexOf( 零角 ) != )                            if(cMoneyStringBuffer indexOf( 零角 )==(cMoneyStringBuffer length() ))                                    cMoneyStringBuffer replace(cMoneyStringBuffer indexOf( 零角 ) cMoneyStringBuffer indexOf( 零角 ) + ZERO);                                else                                    cMoneyStringBuffer delete(cMoneyStringBuffer indexOf( 零角 ) cMoneyStringBuffer indexOf( 零角 ) + );                                        result = cMoneyStringBuffer toString();            return result;       

  private void checkPrecision(String moneyStr)                    int fractionDigits = moneyStr length() moneyStr indexOf(DOT) ;            if (fractionDigits > )                            throw new RuntimeException( 金额 + moneyStr + 的小数位多于两位 ); //精度不能比分低                   

cha138/Article/program/Java/hx/201311/26854

相关参考

知识大全 delphi编程小写金额转换

  财务软件中总少不了大写的金额表示方式如下为一个简单的小写金额转换为大写的函数其思路简单(可以说烂吧居然利用了位置来转换)但是它却几乎可以无限制的转换只要你能读得出来和写得进去  functionT

知识大全 java中IP地址转换十进制数实现代码

java中IP地址转换十进制数实现代码  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  先看实例 

备案表有哪些填制要求?

(1)进口单位代码应填写企业法人代码;  (2)一份备案表,仅允许标注一种结算方式;  (3)付汇币种、金额及金额大写不允许涂改;  (4)付汇金额小写与大写应一致;  (5)备案表填制应字迹规范整齐

知识大全 将数字转为中文金额的大写方式

  publicclassTransRMB  /**  *测试程序的可行性  *@paramargs  */  publicstaticvoidmain(String[]args)  Systemou

知识大全 C#实现阿拉伯数字到大写中文的转换

C#实现阿拉伯数字到大写中文的转换  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  ///<

知识大全 如何在ORACLE中实现人民币大写的转换

如何在ORACLE中实现人民币大写的转换  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  ORAC

知识大全 java字母大小写转换问题

  Java代码  importjavautilScanner;  publicclassWordchange    publicstaticvoidmain(Stringargs[])    Sca

知识大全 25种java模式代码参考手册

  抽象工厂(AbstractFactory)模式意图:为特定的客户(或情况)提供特定系列的对象  类的适配器(Adapter)模式意图:将一个类的接口转换成客户希望的另外一个接口  对象的适配器(A

知识大全 用java实现输入ASCII码,打印出对应的大写字母。

用java实现输入ASCII码,打印出对应的大写字母。  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 java图片处理类(图片水印,图片缩放)

  可实现以下常用功能缩放图像切割图像图像类型转换彩色转黑白文字水印图片水印等  代码如下复制代码importjavaawtAlphaComposite;importjavaawtColor;impo