网站首页 > 文章中心 > 其它

java代码将金额转换_c语言代码转换成java代码

作者:小编 更新时间:2023-10-07 10:33:09 浏览量:215人看过

Java将控制台输入的人民币数字金额转化为大写

代码如下:

/**

java代码将金额转换_c语言代码转换成java代码-图1

* 人民币转成大写

*

* @param value

* @return String

*/

public static String hangeToBig(double value)

{

char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示

char[] vunit = { '万', '亿' }; // 段名表示

char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示

long midVal = (long) (value * 100); // 转化成整形

java代码将金额转换_c语言代码转换成java代码-图2

String valStr = String.valueOf(midVal); // 转化成字符串

String prefix = ""; // 整数部分转化的结果

String suffix = ""; // 小数部分转化的结果

// 处理小数点后面的数

if (rail.equals("00"))

{ // 如果小数部分为0

suffix = "整";

}

else

suffix = digit[rail.charAt(0) - '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来

// 处理小数点前面的数

char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组

char zero = '0'; // 标志'0'表示出现过0

byte zeroSerNum = 0; // 连续出现0的次数

java代码将金额转换_c语言代码转换成java代码-图3

for (int i = 0; i chDig.length; i++)

{ // 循环处理每个数字

if (chDig[i] == '0')

{ // 如果当前字符是0

zeroSerNum++; // 连续0次数递增

if (zero == '0')

{ // 标志

zero = digit[0];

prefix += vunit[vidx - 1];

zero = '0';

continue;

zeroSerNum = 0; // 连续0次数清零

if (zero != '0')

{ // 如果标志不为0,则加上,例如万,亿什么的

prefix += zero;

prefix += digit[chDig[i] - '0']; // 转化该数字表示

if (idx 0)

prefix += hunit[idx - 1];

if (idx == 0 vidx 0)

prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿

if (prefix.length() 0)

prefix += '圆'; // 如果整数部分存在,则有圆的字样

return prefix + suffix; // 返回正确表示

JAVA编程 金额转换

不知道这个是不是您需要的答案

package test.format;

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 TWO = "贰";

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.setGroupingUsed(false);

chineseNumberMap.put("0", ZERO);

chineseNumberMap.put("1", ONE);

chineseNumberMap.put(DOT, DOT);

chineseMoneyPattern.put("1", TEN);

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) {

StringBuffer cMoneyStringBuffer = new StringBuffer();

for (int i = 0; i moneyStr.length(); i++) {

cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));

//拾佰仟万亿等都是汉字里面才有的单位,加上它们

int indexOfDot = cMoneyStringBuffer.indexOf(DOT);

int moneyPatternCursor = 1;

for (int i = indexOfDot - 1; i 0; i--) {

cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));

String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."));

cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length());

while (cMoneyStringBuffer.indexOf("零拾") != -1) {

while (cMoneyStringBuffer.indexOf("零佰") != -1) {

while (cMoneyStringBuffer.indexOf("零仟") != -1) {

while (cMoneyStringBuffer.indexOf("零万") != -1) {

while (cMoneyStringBuffer.indexOf("零亿") != -1) {

while (cMoneyStringBuffer.indexOf("零零") != -1) {

if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)

cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length());

cMoneyStringBuffer.append(fractionPart);

result = cMoneyStringBuffer.toString();

private String addUnitsToChineseMoneyString(String moneyStr) {

StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);

cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);

java 金额转换

private static String chainNum[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",

"柒", "捌", "玖", "拾", "元", "角", "分", "佰", "仟", "万", "亿" };

String tmp = temp.toString();

String chainNum_ = "";

String _chainNum = "";

String[] p = tmp.split("\\.");

String decimalSection = null;

String num = p[0];

if (p.length 1) {

decimalSection = p[1];

if (decimalSection != null) {

if (decimalSection.charAt(0) != '0') {

_chainNum = _chainNum

+ chainNum[Integer.valueOf(Character.valueOf(

decimalSection.charAt(0)).toString())] + "角";

if (decimalSection.charAt(1) != '0') {

decimalSection.charAt(1)).toString())] + "分";

int start = 0;

start = 0;

chainNum_ = chainNum_ + caseChain(yi) + "亿";

chainNum_ = chainNum_ + caseChain(wan) + "万";

if (num.length() 0) {

String wu = num.substring(start, num.length());

chainNum_ = chainNum_ + caseChain(wu) + "元";

return chainNum_ + _chainNum + "整";

private static String caseChain(String tmp) {

String resultValue = "";

for (int i = 0; i status.length; i++) {

status[i] = 0;

if ('0' == tmp.charAt(0)) {

} else {

resultValue = resultValue

+ chainNum[Integer.valueOf(Character.valueOf(tmp.charAt(0))

.toString())] + "仟";

if (tmp.charAt(1) == '0') {

resultValue = resultValue + "零";

+ chainNum[Integer.valueOf(Character.valueOf(tmp.charAt(1))

.toString())] + "佰";

.toString())] + "拾";

.toString())];

return resultValue;

private static String fill0(String tmp, int median) {

int len = tmp.length();

if (len median) {

for (int i = 0; i median - len; i++) {

tmp = "0" + tmp;

return tmp;

将上面代码放到class里面 然后调用

用java编译金额的中文大写转换.

* 金额小数转换成中文大写金额

* @author Neil Han

public class ConvertMoneyToUppercase {

private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",

"拾", "万", "千", "佰", "拾", "元", "角", "分" };

private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",

"柒", "捌", "玖" };

* 将金额小数转换成中文大写金额

* @param money

* @return result

public static String convertMoney(double money) {

if (money 0 || money MAX_VALUE)

return "参数非法!";

long money1 = Math.round(money * 100); // 四舍五入到分

if (money1 == 0)

return "零元整";

String strMoney = String.valueOf(money1);

int numIndex = 0; // numIndex用于选择金额数值

int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位

boolean isZero = false; // 用于判断当前为是否为零

String result = "";

for (; numIndex strMoney.length(); numIndex++, unitIndex++) {

char num = strMoney.charAt(numIndex);

if (num == '0') {

isZero = true;

if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"

|| UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元,且数值为零

result = result + UNIT[unitIndex]; //补单位亿、万、元

isZero = false;

}else {

if (isZero) {

result = result + "零";

result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];

//不是角分结尾就加"整"字

if (!result.endsWith("角")!result.endsWith("分")) {

result = result + "整";

result = result.replaceAll("亿万", "亿");

public static void main(String[] args) {

System.out.println("您输入的金额(小写)为:" + value);

System.out.println("您输入的金额(大写)为:" + convertMoney(value));

java实现金额转换,阿拉伯数字的金额转换成中国传统的形式

直接通过以下接口类方法实现即可:

import java.math.BigDecimal;

* 金额工具类

* @author zn

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);

integerStr = str;

decimalStr = "";

// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)

if (!integerStr.equals("")) {

integerStr = Long.toString(Long.parseLong(integerStr));

if (integerStr.equals("0")) {

// overflow超出处理能力,直接返回

if (integerStr.length() IUNIT.length) {

System.out.println(str + ":超出处理能力");

return str;

int[] integers = toArray(integerStr);// 整数部分数字

int[] decimals = toArray(decimalStr);// 小数部分数字

+ 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;

* 得到中文金额的整数部分.

StringBuffer chineseInteger = new StringBuffer("");

int length = integers.length;

for (int i = 0; i length; i++) {

// 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)

String key = "";

if (integers[i] == 0) {

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++) {

break;

chineseDecimal.append(decimals[i] == 0 ? ""

: (NUMBERS[decimals[i]] + DUNIT[i]));

return chineseDecimal.toString();

int length = integerStr.length();

String subInteger = "";

return Integer.parseInt(subInteger) 0;

return false;

* BigDecimal 相乘,四舍五入保留0位

* @param a

* @param b

* @return a*b

public static BigDecimal mutiply(String a, String b, int roundingMode) {

BigDecimal bd = new BigDecimal(a);

return bd.multiply(new BigDecimal(b)).setScale(DFT_SCALE, roundingMode);

* BigDecimal 相除,四舍五入保留两位

* @return a/b

BigDecimal decimal1 = new BigDecimal(a);

* BigDecimal 相加,四舍五入保留两位

* @return a+b

public static BigDecimal sum(String a, String b, int roundingMode) {

// DecimalFormat format = new DecimalFormat("#0.00");

* BigDecimal 相减,四舍五入保留两位

public static BigDecimal sub(String a, String b, int roundingMode) {

* 100.00 为10000

* @return

public static BigDecimal format(String a, int roundingMode) {

return new BigDecimal(a).multiply(new BigDecimal(100)).setScale(0,

roundingMode);

System.out.println(number + " " + MoneyUtil.toChinese(number));

备注:最后面的main方法是具体的调用.

以上就是土嘎嘎小编为大家整理的java代码将金额转换相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章