随笔 - 25  文章 - 32  trackbacks - 0
<2008年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(2)

随笔档案

文章分类

文章档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

package org.phyeas.demo.number;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberToRMB {
    
private static String[] UNIT = { """""亿" };
    
private static String[] UNIT2 = { """""" };

    
private static final String ZERO = "";

    
private static String[] NUM = { """""""""""""""",
            
"""" };

    
/**
     * 转换一串字符
     * 
     * 
@param str
     * 
@return
     
*/
    
public static String parseCharsToRMB(String str) {
        
if (!isNumber(str)) {
            
throw new IllegalArgumentException("参数不正确,必须为数字");
        }
        StringBuffer buffer 
= new StringBuffer();
        
int count = 0;
        
for (int i = str.length(); i > 0; i -= 4, count++) {
            String char4 
= null;
            
if (i - 4 < 0) {
                System.out.println(
"start=" + 0 + ".end=" + i);
                char4 
= str.substring(0, i);
                buffer.insert(
0, parse4CharsToRMB(char4) + UNIT[count]);
                
break;
            }
            System.out.println(
"start=" + (i - 4+ ".end=" + i);
            char4 
= str.substring(i - 4, i);
            buffer.insert(
0, parse4CharsToRMB(char4) + UNIT[count]);

        }
        
return buffer.toString();
    }

    
/**
     * 转换4位字符
     * 
     * 
@param str
     * 
@return
     
*/
    
public static String parse4CharsToRMB(String str) {
        
if (str.length() > 4 || !isNumber(str)) {
            
throw new IllegalArgumentException("参数不正确,str应该为不大于4位的字符");
        }
        StringBuffer result 
= new StringBuffer();
        
int startUNIT = getStartUNIT(str.length());

        
for (int i = 0; i < str.length(); i++) {
            
if (str.charAt(i) == '0') {
                
if (i == 0) {
                    result.append(ZERO);
                } 
else {
                    appendZero(result, i);
                }
                startUNIT
++;
            } 
else {
                
int index = Integer.parseInt(String.valueOf(str.charAt(i))) - 1;
                result.append(NUM[index]);
                
if (startUNIT < UNIT2.length) {
                    result.append(UNIT2[startUNIT]);
                    startUNIT
++;
                }
            }
        }
        
return result.toString();
    }

    
/**
     * 获取起始位置
     * 
     * 
@param strLength
     * 
@return
     
*/
    
public static int getStartUNIT(int strLength) {
        
switch (strLength) {
        
case 4:
            
return 0;
        
case 3:
            
return 1;
        
case 2:
            
return 2;
        
case 1:
            
return 3;
        
default:
            
return 0;
        }
    }

    
/**
     * 0是特殊的。所以单独一个方法添加0
     * 
     * 
@param buffer
     * 
@param index
     * 
@return
     
*/
    
private static StringBuffer appendZero(StringBuffer buffer, int index) {
        System.out.println(
"buffer=" + buffer);
        String before 
= buffer.substring(index, index + 1);
        System.out.println(
"before=" + before);
        
if (before.equals(ZERO)) {
            
return buffer;
        } 
else {
            buffer.append(ZERO);
        }
        
return buffer;
    }

    
/**
     * 判断是否为数字字符串
     * 
     * 
@param str
     * 
@return
     
*/
    
public static boolean isNumber(String str) {
        
if (str != null && str.length() > 0) {
            Pattern pattern 
= Pattern.compile("[0-9]*");
            Matcher m 
= pattern.matcher(str);
            
if (m.find()) {
                
return true;
            }
            
return false;
        }
        
return false;
    }

    
public static void main(String[] args) {
        System.out.println(
"转换1580654=" + parseCharsToRMB("1580654"));
    }
}
posted on 2008-06-30 20:38 phyeas 阅读(368) 评论(3)  编辑  收藏

FeedBack:
# re: 将数字形式转换为人民币大写形式的程序实现 2009-01-21 01:46 人民币汇率
转换1580654  回复  更多评论
  
# re: 将数字形式转换为人民币大写形式的程序实现 2009-01-31 09:05 phyeas
@人民币汇率
?  回复  更多评论
  
# re: 将数字形式转换为人民币大写形式的程序实现 2010-09-20 16:08 cxx
private string GetString(double num)
{
double intit=num;
long zhengshu=Convert.ToInt64(num*100);
string str=zhengshu.ToString();
char[] arrCha=str.ToCharArray();
StringBuilder strb=new StringBuilder();
int jj=0;
for(int i=0;i<arrCha.Length;i++)
{
jj=i;
if(jj>=7&&jj<=9)
{
jj-=4;
}
else if(jj==10)
{
jj-=3;
}
else if(jj>=11)
{
jj-=7;
}

strb.Insert(0,((Chan)(Convert.ToInt32((arrCha[arrCha.Length-1-i]).ToString()))).ToString()+((ChanWei)jj).ToString());
}
return strb.ToString();
}
private enum Chan{零,壹,贰,叁,肆,伍,陆,柒,捌,玖}
private enum ChanWei{分,角,元,拾,百,千,万,亿}

这个如何  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: