/**
*金额大小写转换工具类
*/
public class MoneyUtil {
/** 大写数字 */
private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
/** 整数部分的单位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰",
"仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
/** 小数部分的单位 */
private static final String[] DUNIT = { "角", "分", "厘" };
/**
* 得到大写金额。
*/
public static String toChinese(String str) {
str = str.replaceAll(",", "");// 去掉","
String integerStr;// 整数部分数字
String decimalStr;// 小数部分数字
// 初始化:分离整数部分和小数部分
if (str.indexOf(".") > 0) {
integerStr = str.substring(0, str.indexOf("."));
decimalStr = str.substring(str.indexOf(".") + 1);
} else if (str.indexOf(".") == 0) {
integerStr = "";
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = "";
}
// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
}
// overflow超出处理能力,直接返回
if (integerStr.length() > IUNIT.length) {
System.out.println(str + ":超出处理能力");
return str;
}
int[] integers = toArray(integerStr);// 整数部分数字
boolean isMust5 = isMust5(integerStr);// 设置万单位
int[] decimals = toArray(decimalStr);// 小数部分数字
return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
}
/**
* 整数部分和小数部分转换为数组,从高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}
/**
* 得到中文金额的整数部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i < length; i++) {
// 0出现在关键位置:1234(万)5678(亿)9012(万)3456(元)
// 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// 万(亿)(必填)
key = IUNIT[4];
else if ((length - i) == 9)// 亿(必填)
key = IUNIT[8];
else if ((length - i) == 5 && isMust5)// 万(不必填)
key = IUNIT[4];
else if ((length - i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0时补零,不包含最后一位
if ((length - i) > 1 && integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key
: (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
}
return chineseInteger.toString();
}
/**
* 得到中文金额的小数部分。
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i < decimals.length; i++) {
// 舍去3位小数之后的
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? ""
: (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}
/**
* 判断第5位数字的单位"万"是否应加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) {
// 取得从低位数,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}
public static void main(String[] args) {
// MoneyUtil moneyUtil = new MoneyUtil();
// System.out.println(moneyUtil.toChinese("10001"));
}
}

package com.shaofei.test;

import java.util.HashMap;
import java.util.Map; public class CNNMFilter {
private static final Character[] CN_NUMERIC = { '一', '二', '三', '四', '五',
'六', '七', '八', '九', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖',
'十', '百', '千', '拾', '佰', '仟', '万', '亿', '○', 'O', '零' }; private static Map<Character, Integer> cnNumeric = null; static {
cnNumeric = new HashMap<Character, Integer>(40, 0.85f);
for (int j = 0; j < 9; j++)
cnNumeric.put(CN_NUMERIC[j], j + 1);
for (int j = 9; j < 18; j++)
cnNumeric.put(CN_NUMERIC[j], j - 8);
cnNumeric.put('两', 2);
cnNumeric.put('十', 10);
cnNumeric.put('拾', 10);
cnNumeric.put('百', 100);
cnNumeric.put('佰', 100);
cnNumeric.put('千', 1000);
cnNumeric.put('仟', 1000);
cnNumeric.put('万', 10000);
cnNumeric.put('亿', 100000000);
}
public static int isCNNumeric(char c) {
Integer i = cnNumeric.get(c);
if (i == null)
return -1;
return i.intValue();
}
public static int cnNumericToArabic(String cnn, boolean flag) { cnn = cnn.trim();
if (cnn.length() == 1)
return isCNNumeric(cnn.charAt(0)); if (flag)
cnn = cnn.replace('佰', '百').replace('仟', '千').replace('拾', '十')
.replace('零', ' ');
// System.out.println(cnn);
int yi = -1, wan = -1, qian = -1, bai = -1, shi = -1;
int val = 0;
yi = cnn.lastIndexOf('亿');
if (yi > -1) {
val += cnNumericToArabic(cnn.substring(0, yi), false) * 100000000;
if (yi < cnn.length() - 1)
cnn = cnn.substring(yi + 1, cnn.length());
else
cnn = ""; if (cnn.length() == 1) {
int arbic = isCNNumeric(cnn.charAt(0));
if (arbic <= 10)
val += arbic * 10000000;
cnn = "";
}
} wan = cnn.lastIndexOf('万');
if (wan > -1) {
val += cnNumericToArabic(cnn.substring(0, wan), false) * 10000;
if (wan < cnn.length() - 1)
cnn = cnn.substring(wan + 1, cnn.length());
else
cnn = "";
if (cnn.length() == 1) {
int arbic = isCNNumeric(cnn.charAt(0));
if (arbic <= 10)
val += arbic * 1000;
cnn = "";
}
} qian = cnn.lastIndexOf('千');
if (qian > -1) {
val += cnNumericToArabic(cnn.substring(0, qian), false) * 1000;
if (qian < cnn.length() - 1)
cnn = cnn.substring(qian + 1, cnn.length());
else
cnn = "";
if (cnn.length() == 1) {
int arbic = isCNNumeric(cnn.charAt(0));
if (arbic <= 10)
val += arbic * 100;
cnn = "";
}
} bai = cnn.lastIndexOf('百');
if (bai > -1) {
val += cnNumericToArabic(cnn.substring(0, bai), false) * 100;
if (bai < cnn.length() - 1)
cnn = cnn.substring(bai + 1, cnn.length());
else
cnn = "";
if (cnn.length() == 1) {
int arbic = isCNNumeric(cnn.charAt(0));
if (arbic <= 10)
val += arbic * 10;
cnn = "";
}
} shi = cnn.lastIndexOf('十');
if (shi > -1) {
if (shi == 0)
val += 1 * 10;
else
val += cnNumericToArabic(cnn.substring(0, shi), false) * 10;
if (shi < cnn.length() - 1)
cnn = cnn.substring(shi + 1, cnn.length());
else
cnn = "";
} cnn = cnn.trim();
for (int j = 0; j < cnn.length(); j++)
val += isCNNumeric(cnn.charAt(j))
* Math.pow(10, cnn.length() - j - 1); return val;
} public static int qCNNumericToArabic(String cnn) {
int val = 0;
cnn = cnn.trim();
for (int j = 0; j < cnn.length(); j++)
val += isCNNumeric(cnn.charAt(j))
* Math.pow(10, cnn.length() - j - 1);
return val;
} public static void main(String[] args) {
int val = 0;
long s = System.nanoTime();
val = cnNumericToArabic("三亿二千零六万七千五百六", true);
System.out.println(val);
val = cnNumericToArabic("一九九八", true);
System.out.println(val);
long e = System.nanoTime();
System.out.format("Done[" + val + "], cost: %.5fsec\n",
((float) (e - s)) / 1E9);
} }

java 金额的大小写转换类的更多相关文章

  1. Java对字母大小写转换

    Java对字母大小写转换 1.小写——大写String aa = "abc".toUpperCase(); 2.大写——小写 String bb = "ABC" ...

  2. Java练习 SDUT-2746_大小写转换

    大小写转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description X现在要学习英文以及各种稀奇古怪的字符的了.现在他想把一串字 ...

  3. 020、Java中字母大小写转换

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...

  5. 日期转换类 DateConverter.java

    package com.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.D ...

  6. 一个java的DES加解密类转换成C#

    原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //i ...

  7. Java字节转换类实现

    Java的类库支持完全不如C#,比如时间类,比如数据类型转换类等等,难道是我自己没找到吗? 下面是字节转换类,byte[]与short, int, long, float, double, Strin ...

  8. Java 大小写转换

    Java 大小写转换 public class CaseConversion { /** * @param character: a character * @return: a character ...

  9. java代码把字母转换大小写、、、、

    总结:从键盘输入多少次,就用for循环控制.这些需要输入的数据都放在循环内部,否则不会执行多次. package com.aaa; import java.util.Scanner; //大小写字母的 ...

随机推荐

  1. Delphi线程类 DIY(把类指针作为参数传进去,就可以执行类里面的方法啦)

    Delphi 封装了一个很强大的线程类 TThread, 我们也自己动手制作一个简单的线程类 首先Type一个类 type TwwThread = class constructor Create;  ...

  2. TApplicationEvents的前世今生(待续)

    这是它的声明,它的数据成员全部都是Event,而没有真正意义上的数据(如此一来,几乎可以猜测,它本身什么都做不了): TCustomApplicationEvents = class(TCompone ...

  3. qt 维护x86和arm两套编译环境

    1.中间库: 中间库都放在middlewares目录,include头文件相同,所以不需要特殊处理,只要特殊处理lib安装目录, 示例pro文件如下: TEMPLATE = lib TARGET = ...

  4. vs2010添加TSTCON( ActiveX Control Test Container )工具

    vs2010中的TSTCON( ActiveX Control Test Container )工具非自动安装,而是作为一个例程提供.所以应找到该例程,并编译: 如vs2010安装在默认路径则 1, ...

  5. sublime3使用笔记

    1.ctrl+n 新建一个文件: 2.alt+shift+数字 分屏显示: 3.ctrl+alt+down(向下键) 连选很多行的指定开始位置: 如图: 紧接着再按shift+right(选中需要更改 ...

  6. 《HelloGitHub》第 39 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  7. 如何使用jQuery可以让滚轮滚到底部可以自动加载所需内容

    话不多说先上代码 $(window).scroll(function() {   var scrollTop = $(this).scrollTop(); //滚动高度  var windowHeig ...

  8. Scala 学习之路(八)—— 类和对象

    一.初识类和对象 Scala的类与Java的类具有非常多的相似性,示例如下: // 1. 在scala中,类不需要用public声明,所有的类都具有公共的可见性 class Person { // 2 ...

  9. Ruby中的数值

    数值类型 Ruby中所有数值都是Numeric类的子类对象,数值都是不可变对象. 数值类型的继承关系如下: Integer是整数,Float是浮点数类型,Rational是分数. 对于整数,要么是Fi ...

  10. 使用Appium做手机自动化录制问题

    最近在使用appium做Android手机自动化脚本录制, 发现点击“tap”时,一直没有用,页面还是不能跳转. 咋办?发愁... 于是看到旁边有个“sendkeys”,那是不是能够直接发送参数不就行 ...