一个金额大小写问题

awincsdn 2003-06-11 06:24:02
现有数据为String 型小写金额。形式例如1.00,1453.00等。
打印的时候要把它转换为大写金额。形式例如壹百零贰元整,壹亿元叁角贰分等形式。
要达到百亿。请问如何写这个函数。
...全文
385 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
研诺信息技术 2010-06-07
  • 打赏
  • 举报
回复
public static String RMBtoString(double x) //将x表示的金额转换成中文大写形式
{
String yuan="亿千百拾万千百拾元角分";
String digit="零壹贰叁肆伍陆柒捌玖"; //字符串长度digit.length()为10
String result="";
int y=(int)Math.round(x*100-0.5); //浮点数扩充100倍后取整(保留两位小数)
int i=yuan.length()-1;
while (y>0 && i>0)
{
result = ""+digit.charAt(y % 10)+yuan.charAt(i)+result; //转换最低位,一个汉字为一个Unicode字符
i--;
y=y/10;
}
return result;
}
s_lim 2003-07-22
  • 打赏
  • 举报
回复
关注……
mem_fox 2003-07-20
  • 打赏
  • 举报
回复
考虑从NumberFormat中扩展一个吧!!这样通用性是不是好一点啦?
liwon 2003-07-01
  • 打赏
  • 举报
回复
能实现就好,象这种东西,谈结构和扩展性完成扯淡,人家写出来,我们要尊敬人家的东西!
suwu 2003-07-01
  • 打赏
  • 举报
回复
高手和低手的差别就在于程序写得好不好。
jwd 2003-06-28
  • 打赏
  • 举报
回复
可以看看:http://expert.csdn.net/Expert/topic/1710/1710886.xml?temp=.8624994
yuantiou 2003-06-19
  • 打赏
  • 举报
回复
dooby(德鲁比):随便了,如果你能改一些的话,那就更强了。

对我来说,无所谓。只不过是提出一个建议罢了
awincsdn 2003-06-19
  • 打赏
  • 举报
回复
多谢大家
dooby 2003-06-18
  • 打赏
  • 举报
回复
yuantiou(仰望星空) :不好总比不作要强得多。
UserJavaPerson 2003-06-18
  • 打赏
  • 举报
回复
public static String toCaption(double d)
{
//大写数字的数组
String as[] = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
};
//定义大写货币单位的排列数组
String as1[] = {
"仟", "佰", "拾", "万", "仟", "佰", "拾", "亿", "仟", "佰",
"拾", "万", "仟", "佰", "拾", "元", "角", "分"
};

String as2[] = new String[40];
String as3[] = new String[40];
double d1 = 0.0D;
int ai[] = new int[20];
double d4 = 1000000000000000D;
StringBuffer stringbuffer = new StringBuffer("");
String s;
//判断待转换金额是否等于零
if(d == 0.0D) {
s = "零元整";
}
else{
//将小数点删除
d *= 100D;
//获得d4的数量级,即d所在的数量级(如:d为1200098.0,d4为1000000)
for(; d4 >= 1.0D; d4 /= 10D){
double d2 = d / d4;
if(d2 >= 1.0D)
break;
}

int i;
//将d值按位进行截取并存放到数组中
for(i = 0; d4 >= 1.0D; i++){
//获得d整除后的值,如1.200098
double d3 = d / d4;
//截取整数并放到数组中,如1,2,0,....
ai[i] = (int)d3;
//取d余数,如200098,00098,...
d %= d4;
//整除,将d4的数量级减小,如:10000,1000,100,...
d4 /= 10D;
}
//获得数值的长度
int i1 = i;
//转换后大写金额的长度
int k = i1 * 2 - 1;
//货币单位数组的长度
int l = 17;
for(i = i1 - 1; i >= 0; i--){
//将货币单位加到数组中,从数组中的最后一个值开始,如:分,角,...
as2[k] = as1[l];
k--;
//将金额转换成大写并放到数组中,如,玖,捌,零,......
as2[k] = as[ai[i]];
k--;
l--;
}

i1 *= 2;
k = 0;
l = 0;
for(i = 0; i < i1; i++){
//判断数组中是否有零的值
if(as2[i].compareTo("零") == 0){
//判断是否到数组的最后一位
if(as2[i + 1] != null){
//判断零后的字符是否为万,亿,元三个字符
if(as2[i + 1].compareTo("万") != 0 && as2[i + 1].compareTo("亿") != 0 && as2[i + 1].compareTo("元") != 0){
as3[k] = as2[i];
i++;
k++;
}
else{
i++;
as3[k] = as2[i];
k++;
}
}
}
else{
//生成新的数组,如,壹万捌千元整
as3[k] = as2[i];
k++;
}
}


for(k = 0; as3[k] != null; k++){
//将金额数组中的零值替换为"_"
if(as3[k].compareTo("零") == 0 && as3[k + 1] != null && (as3[k + 1].compareTo("万") == 0 || as3[k + 1].compareTo("亿") == 0 || as3[k + 1].compareTo("元") == 0 || as3[k + 1].compareTo("零") == 0))
as3[k] = "_";
}

//将金额数组中的倒数第二个的零值替换为"_"
if(as3[k - 1].compareTo("零") == 0)
as3[k - 1] = "_";

k = 0;
i = 0;

for(; as3[k] != null; k++){
//将数组中的"_"删除
if(as3[k].compareTo("_") != 0)
{
as2[i] = as3[k];
i++;
}
}

i1 = i;
String ss ="" ;
//生成金额转换后的大写字符串
for(int j = 0; j < i1; j++)
{
stringbuffer.append(as2[j]);
if(as2[j].compareTo("亿") == 0 && as2[j + 1].compareTo("万") == 0)
as2[j + 1] = "";
}

s = stringbuffer.append("整").toString();
}
return s;
}
yuantiou 2003-06-16
  • 打赏
  • 举报
回复
dooby,说话不要这么横。

你的代码写的风格确实不太好,大方法和switch这两个都是让人感觉不舒服的东西。
dooby 2003-06-15
  • 打赏
  • 举报
回复
懒得写就不要在这里说话,是不会还是别的原因那
feiyuegaoshan 2003-06-15
  • 打赏
  • 举报
回复
楼上写的比先前那个好多了:)

我实在是懒得写这样的代码。:)
oldfisher 2003-06-14
  • 打赏
  • 举报
回复
代码有错,真不好意思 我刚发现的 :(
比如2003 我那程序会转为贰仟零佰零拾参的,
对数字中间的零没做处理,请大伙帮忙改正
HuangBin 2003-06-14
  • 打赏
  • 举报
回复
/**
* 功能:数字 变成 汉字
* 汉字 变成 数字
* -----------------------------------
* 说明: 只能处理1000亿范围内的数据.
* 小数点后面最多保留9位
* -----------------------------------
* 如:
* 123 变成 零佰贰拾叁
* 零佰贰拾叁 变成 123
*
* 需求分析:
* ====================
* 数字到汉字
* ====================
* 数字的组成部分: 1: 符号位: 取值范围 {"正","负"}
* 2: 整数部分
* 3: 小数部分
* 4: 整数部分和小数部分之间的分割符号"."
* A:符号位影射到汉字比较简单 “+”----"正"
* "-" ----"负"
* B:分割符号"."的影射是最简单的了 "." ---"点"
* C:小数部分的影射也很简单,直接"翻译"
* .234 ---点贰叁肆.
* D:整数部分的影射复杂点.
* 整数部分的”翻译“之所以复杂是因为除了要将数字影射到对应的汉字,还
* 要添加一些"修饰词"(我把拾,佰,仟,万,亿称做修饰词).
* 如: 1234123421
* 对应汉字: 1拾2亿3仟4佰1拾2万3仟4佰2拾1.(输汉字好麻烦,所以我没有将
* 数字”翻译“过来.
* 如果你仔细分析一下,你会发现,这样的翻译工作很简单.
* 请从 ”1拾2亿3仟4佰1拾2万3仟4佰2拾1“取出全部修饰符(依次从左往右取)
* 你会发现取出来的修饰符号,会很有规律,看上去一定是这个样子:
* 拾,佰,仟,万,拾,佰,仟,亿,拾,佰,仟
* 实现很简单,请看源代码.
* ---------------------------------------------------------------------------
* ====================
* 汉字到数字
* ====================
* 现在发现这样的处理没有什么实际意义。所以我就不写了.
* ---------------------------------------------------------------------
* @version 1.00
* @author huangyh
* @email huangbin0791@163.com
* @date 2002.10.11
*/
package com.cxlife.convert;
public class Convert {

private final static String[] a_strNumber = {"零","壹","贰","叁","肆",
"伍","陆","柒","捌","玖"};
private final static String[] a_strModify = {"","拾","佰","仟",
"万","拾","佰","仟",
"亿","拾","佰","仟"};

private final static String strSign = "负";//实际上”+“号永远都不可能出现.

private final static String strDot = "点";

/**
* 功能: 提取符号位.
* 说明: 如果输入参数是 "-13.3",调用该函数的返回值是 "负";
* 如果输入参数是 "13.3", 调用该函数的返回值是 ""(空值).
* @param pValue
*
*/
static private String getSign(String pValue){
return pValue.indexOf("-") == 0 ? "负" : "";
}

/**
* 功能:返回小数部分的汉字
* 说明:如果输入数据是 12.35,调用该函数返回值是 叁伍
*
* @param pValue
* @return
*/
static private String getFraction(String pValue){
String strFraction = null;//用来保存小数部分的数字串
int intDotPos = pValue.indexOf(".");
if (intDotPos == -1)//没有小数部分.
return "";
strFraction = pValue.substring(intDotPos +1);
StringBuffer sbResult = new StringBuffer(strFraction.length());
//开始翻译.
for(int i=0; i<strFraction.length(); i++)
sbResult.append(a_strNumber[strFraction.charAt(i) - 48]);
return sbResult.toString();
}

/*
* 功能: 返回整数部分的汉字.
* 如果输入参数是: 234.3,调用该函数的返回值是 贰佰叁拾肆.
* @param pValue
* @return
*/
static private String getInteger(String pValue){
String strInteger = null;//用来保存整数部分数字串
int intDotPos = pValue.indexOf(".");//记录"."所在位置
int intSignPos = pValue.indexOf("-");
if (intDotPos == -1 )
intDotPos = pValue.length();

strInteger = pValue.substring(intSignPos+1, intDotPos);

//反转整数部分数据
strInteger = new StringBuffer(strInteger).reverse().toString();
//-----------------------------------------------------------
//开始翻译:
StringBuffer sbResult = new StringBuffer();
for(int i=0; i<strInteger.length(); i++) {
sbResult.append(a_strModify[i]);
sbResult.append(a_strNumber[strInteger.charAt(i) - 48]);
}

sbResult = sbResult.reverse();
//这个时候得到的结果不标准,需要调整.
//203返回值是 贰佰零拾三个 正确答案是 贰佰零三

//--------------------------------------------------------------------------
//串调整.
replace(sbResult,"零拾","零");
replace(sbResult,"零佰","零");
replace(sbResult,"零仟","零");
replace(sbResult,"零万","万");
replace(sbResult,"零亿","亿");
//多个”零“调整处理
replace(sbResult,"零零","零");
replace(sbResult,"零零零","零");
replace(sbResult,"零零零零万","");//这两句不能颠倒顺序
replace(sbResult,"零零零零","");

replace(sbResult,"壹拾亿","拾亿");//这样读起来更习惯.
replace(sbResult,"壹拾万","拾万");

//--------------------------------------------------------------------------

if (sbResult.charAt(sbResult.length()-1) == '零'&&
sbResult.length() != 1)//删除个位上的零
sbResult.deleteCharAt(sbResult.length()-1);

if (strInteger.length() == 2){
replace(sbResult,"壹拾","拾");
}

return sbResult.toString();//将结果反转回来.
}

/**
* 功能: 返回分割符号
* 如果参数是"12.3" 调用该函数的返回值是"点"
* 如果参数是"12" 调用该函数的返回值是""(空值)
* @param pValue
* @return
*/
static private String getDot(String pValue){
return pValue.indexOf(".") != -1 ? "点" : "";
}

//数字到汉字
static public String NumberToChinese(double pValue){
//注意:不能用string.valueOf(pValue)处理,你自己试试就知道了.
java.text.DecimalFormat df = new java.text.DecimalFormat("#.#########");
String pTemp = String.valueOf( df.format(pValue));
StringBuffer sbResult = new StringBuffer(getSign(pTemp) + getInteger(pTemp) +
getDot(pTemp) + getFraction(pTemp));
return sbResult.toString();

}

/**
* 功能:用给定字符串pDest替换字符串pValue中的pSource
* @param pValue
* @param pSource
* @param pDest
* @return 经过替换处理的字符串
* 例:pValue= xy ,pSource =x ,pDest = 测试
* 调用改函数后pValue =测试y
*
* 说明一下: 如果 pvalue= xxx pSource = xx 处理结果是 x
* ,这个结果可能与您平时看到的替换函数有点不一样,通常应该是
* pSource =xx.
*
*/
static private void replace(StringBuffer pValue, String pSource, String pDest){
if (pValue == null ||
pSource == null ||
pDest == null)
return ;

int intPos = 0;//记录pSource在pValue中的位置
do{
//---------------------------------------------------
//intPos = pValue.toString().indexOf(pSource,intPos);
//---------------------------------------------------

//============================================
intPos = pValue.toString().indexOf(pSource);
//============================================
if (intPos == -1)//没有找到pSource.
break;
pValue.delete(intPos,intPos + pSource.length());
pValue.insert(intPos,pDest);

//---------------------------------
//intPos += pSource.length();
//---------------------------------
}while(true);
}
//现在发现这样的处理没有什么实际意义。所以我就不写了.
public String ChineseToNumber(String pValue){
return null;
}


public static void main(String[] args) {
//为了方便您看程序运行结果,我将所有方法都设置成静态的

//测试:

}
}

oldfisher 2003-06-13
  • 打赏
  • 举报
回复
public static String getCN_Money(String money)
throws java.lang.NumberFormatException
{
money = trim(money.trim(),"0",false);
money = trim(money,".",false);//去掉首尾多余的0和.

if((money==null)||(money.trim().equals(""))) return "零元整";
String intFraction = ""; //整数部分
String decimalFraction = ""; //小数部分

if(money.indexOf(".")>0){
intFraction = money.substring(0,money.indexOf("."));
decimalFraction = money.substring(money.indexOf(".")+1);
}else{
intFraction = money;
}
String cn_money = getIntFraction(intFraction)+"元"+getDecimalFraction(decimalFraction)+"整";
return cn_money;
}

public static String getIntFraction(String intFraction)
{
String str = "";
if((intFraction!=null)&&(!intFraction.trim().equals(""))){
if(intFraction.length()>12){
String limitPart = intFraction.substring(intFraction.length()-12);
intFraction = intFraction.substring(0,intFraction.length()-12);
str = getIntFraction(intFraction)+"兆"+getLimitMoney(limitPart);
}else{
str = getLimitMoney(intFraction);
}
}
return str;
}

public static String getDecimalFraction(String decimalFraction)
{
String str = "";
if((decimalFraction!=null)&&(!decimalFraction.trim().equals(""))){
if(decimalFraction.length()>2){
decimalFraction = decimalFraction.substring(0,2);
}
String[] wordArr = new String[] {"零","壹","贰","参","肆","伍","陆","柒","捌","玖"};
String[] unitArr = new String[] {"角","分"};
for(int i=0; i<decimalFraction.length(); i++){
str += wordArr[Integer.parseInt(decimalFraction.substring(i,i+1))]+unitArr[i];
}
}
return str;
}

private static String getLimitMoney(String money)
{
//兆以下部分的
String str = "";
if((money!=null)&&(!money.trim().equals(""))){
String[] unitArr = new String[] {"","万","亿"};
int sect = 0;
if((money.length() % 4)==0) sect = (money.length() / 4);
else sect = (money.length() / 4)+1;
for(int i=0; i<sect; i++){
int p1 = money.length()-4*(i+1);
int p2 = money.length()-4*i;
if(p1<0) p1 = 0;
String part = money.substring(p1,p2);
str = getFourFraction(part)+unitArr[i]+str;
}
}
return str;
}

private static String getFourFraction(String fraction)
{
//四为数字的
String str = "";
if((fraction!=null)&&(!fraction.trim().equals(""))){
String[] wordArr = new String[] {"零","壹","贰","参","肆","伍","陆","柒","捌","玖"};
String[] unitArr = new String[] {"","拾","佰","仟"};
for(int i=0; i<fraction.length(); i++){
str = wordArr[Integer.parseInt(fraction.substring(fraction.length()-i-1,fraction.length()-i))]+unitArr[i]+str;
}
}
return str;
}

public static void main(String[] args)
{
System.out.println(getCN_Money("1223452346246256252354231345678912340.678"));
//System.out.println(getLimitMoney("345678912340"));
//System.out.println(getFourFraction("5678"));
}

前些天刚自己写的 还没怎么测试过 算法也不知道对不对 有什么错请告诉我
dooby 2003-06-12
  • 打赏
  • 举报
回复
大哥 这种东西你扩展它干吗啊 哪你写一个啊。
feiyuegaoshan 2003-06-12
  • 打赏
  • 举报
回复
楼上的程序写的不好。
扩展性太差了。

feiyuegaoshan 2003-06-12
  • 打赏
  • 举报
回复
给我也来一个吧!谢谢!
httruly 2003-06-12
  • 打赏
  • 举报
回复
给我也发一份吧 httruly@sina.com.cn
加载更多回复(7)

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧