java中金额转换分转元,java 字符串转金额

本文目录一览:

java 金额转换

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

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

private static String num2chainNum(String temp) {

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

decimalSection = fill0(decimalSection, 2);

if (decimalSection.charAt(0) != ‘0’) {

_chainNum = _chainNum

+ chainNum[Integer.valueOf(Character.valueOf(

decimalSection.charAt(0)).toString())] + “角”;

}

if (decimalSection.charAt(1) != ‘0’) {

_chainNum = _chainNum

+ chainNum[Integer.valueOf(Character.valueOf(

decimalSection.charAt(1)).toString())] + “分”;

}

}

int start = 0;

if (num.length() 8) {

start = 0;

if (num.length() 12) {

start = num.length() – 12;

}

String yi = num.substring(start, num.length() – 8);

chainNum_ = chainNum_ + caseChain(yi) + “亿”;

}

if (num.length() 4) {

start = 0;

if (num.length() 8) {

start = num.length() – 8;

}

String wan = num.substring(start, num.length() – 4);

chainNum_ = chainNum_ + caseChain(wan) + “万”;

}

if (num.length() 0) {

start = 0;

if (num.length() 4) {

start = num.length() – 4;

}

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

chainNum_ = chainNum_ + caseChain(wu) + “元”;

}

return chainNum_ + _chainNum + “整”;

}

// 只能传4位数

private static String caseChain(String tmp) {

tmp = fill0(tmp, 4);

String resultValue = “”;

byte status[] = new byte[5];

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

status[i] = 0;

}

if (‘0’ == tmp.charAt(0)) {

} else {

status[4] = -1;

resultValue = resultValue

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

.toString())] + “仟”;

}

if (tmp.charAt(1) == ‘0’) {

if (status[4] == 0) {

} else {

if (tmp.charAt(2) != ‘0’ || tmp.charAt(3) != ‘0’)

resultValue = resultValue + “零”;

}

} else {

status[3] = -1;

resultValue = resultValue

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

.toString())] + “佰”;

}

if (tmp.charAt(2) == ‘0’) {

status[2] = 0;

if ((status[4] == 0 status[3] == 0)

|| (status[4] != 0 status[3] == 0)) {

} else {

if (tmp.charAt(2) != ‘0’ || tmp.charAt(3) != ‘0’)

resultValue = resultValue + “零”;

}

} else {

resultValue = resultValue

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

.toString())] + “拾”;

}

if (tmp.charAt(3) != ‘0’) {

resultValue = resultValue

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

.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里面 然后调用

num2chainNum(“11231.120”)

JAVA编程 金额转换

/**

金额转换,阿拉伯数字的金额转换成中国传统的形式如:

(¥1011)-(一千零一拾一元 整)输出。

*/

import java.io.*;

import java.lang.String;

public class Money{

public static void main(String[] args)throws Exception{

String str=null;

System.out.println(“请输入您的金额¥:”);

flag:

while(true){

try{BufferedReader in=

new BufferedReader(new InputStreamReader(System.in));

str=in.readLine();

}catch(IOException e){}

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

if(str.charAt(i)57||str.charAt(i)48){

System.out.println(“您输入的金额有误!请重新输入”);

continue flag;

}

}

break;

}

char[] ch=str.toCharArray();

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

switch(ch[i]){

case ‘0’:{ ch[i]=’零’; break;}

case ‘1’:{ ch[i]=’壹’; break;}

case ‘2’:{ ch[i]=’贰’; break;}

case ‘3’:{ ch[i]=’叁’; break;}

case ‘4’:{ ch[i]=’肆’; break;}

case ‘5’:{ ch[i]=’伍’; break;}

case ‘6’:{ ch[i]=’陆’; break;}

case ‘7’:{ ch[i]=’柒’; break;}

case ‘8’:{ ch[i]=’捌’; break;}

case ‘9’:{ ch[i]=’玖’; break;}

default: ch[i]=’f’;

}

}

int i=0;

switch(ch.length){

case 0:

case 1: {System.out.println(ch[i]+”元整”);}

case 2: {System.out.println(ch[i]+”十”+ch[i+1]+”元整”);}

case 3: {System.out.println(ch[i]+”百”+ch[i+1]+”十”+ch[i+2]+”元整”);}

case 4: {System.out.println(ch[i]+”千”+ch[i+1]+”百”+ch[i+2]+”十”

+ch[i+3]+”元整”); break;}

case 5: {System.out.println(ch[i]+”万”+ch[i+1]+”千”+ch[i+2]+”百”

+ch[i+3]+”十”+ch[i+4]+”元整”); break;}

case 6: {System.out.println(ch[i]+”十”+ch[i+1]+”万”+ch[i+2]+”千”

+ch[i+3]+”百”+ch[i+4]+”十”+ch[i+5]+”元整”); break;}

case 7: {System.out.println(ch[i]+”百”+ch[i+1]+”十”+ch[i+2]+”万”

+ch[i+3]+”千”+ch[i+4]+”百”+ch[i+5]+”十”+ch[i+6]+”元整”); break;}

case 8: {System.out.println(ch[i]+”千”+ch[i+1]+”百”+ch[i+2]+”十”

+ch[i+3]+”万”+ch[i+4]+”千”+ch[i+5]+”百”+ch[i+6]+”十”+ch[i+7]+”元整”); break;}

case 9: {System.out.println(ch[i]+”亿”+ch[i+1]+”千”+ch[i+2]+”百”

+ch[i+3]+”十”+ch[i+4]+”万”+ch[i+5]+”千”+ch[i+6]+”百”+ch[i+7]+”十”

+ch[i+8]+”元整”); break;}

case 10: {System.out.println(ch[i]+”十”+ch[i+1]+”亿”+ch[i+2]+”千”

+ch[i+3]+”百”+ch[i+4]+”十”+ch[i+5]+”万”+ch[i+6]+”千”+ch[i+7]+”百”+ch[i+8]+”十”

+ch[i+9]+”元整”); break;}

default: System.out.println(“错误”);

}

}

}

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

/**

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

* @author Neil Han

*

*/

public class ConvertMoneyToUppercase {

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

“拾”, “万”, “千”, “佰”, “拾”, “元”, “角”, “分” };

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

“柒”, “捌”, “玖” };

private static final double MAX_VALUE = 9999999999999.99D;

/**

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

* @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 + “零”;

isZero = false;

}

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

}

}

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

if (!result.endsWith(“角”)!result.endsWith(“分”)) {

result = result + “整”;

}

//例如没有这行代码,数值”400000001101.2″,输出就是”肆千亿万壹千壹佰零壹元贰角”

result = result.replaceAll(“亿万”, “亿”);

return result;

}

public static void main(String[] args) {

double value = Double.parseDouble(“40330701101.2”);

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

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

}

}

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

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

import java.math.BigDecimal;

/**

* 金额工具类

*

* @author zn

*

* @Date 2013-2-1

* @Email zn.share@gmail.com

*/

public class MoneyUtil {

private static final int DFT_SCALE = 2;

/** 大写数字 */

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) { // TODO 12-9-17

// 取得从低位数,第5到第8位的字串

subInteger = integerStr.substring(length – 8, length – 4);

} else {

subInteger = integerStr.substring(0, length – 4);

}

return Integer.parseInt(subInteger) 0;

} else {

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 相除,四舍五入保留两位

*

* @param a

* @param b

* @return a/b

*/

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

BigDecimal decimal1 = new BigDecimal(a);

BigDecimal decimal2 = new BigDecimal(b);

return decimal1.divide(decimal2, DFT_SCALE, roundingMode);

}

/**

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

*

* @param a

* @param b

* @return a+b

*/

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

BigDecimal decimal1 = new BigDecimal(a);

BigDecimal decimal2 = new BigDecimal(b);

// DecimalFormat format = new DecimalFormat(“#0.00”);

return decimal1.add(decimal2).setScale(DFT_SCALE, roundingMode);

}

/**

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

*

* @param a

* @param b

* @return a+b

*/

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

BigDecimal decimal1 = new BigDecimal(a);

BigDecimal decimal2 = new BigDecimal(b);

// DecimalFormat format = new DecimalFormat(“#0.00”);

return decimal1.subtract(decimal2).setScale(DFT_SCALE, roundingMode);

}

/**

* 100.00 为10000

*

* @param a

* @return

*/

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

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

roundingMode);

}

public static void main(String[] args) {

String number = “54452”;

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

number = “30200”;

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

number = “30000.05”;

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

number = “30000.00”;

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

}

}

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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/297168.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-28 12:15
下一篇 2024-12-28 12:15

相关推荐

  • 金额选择性序列化

    本文将从多个方面对金额选择性序列化进行详细阐述,包括其定义、使用场景、实现方法等。 一、定义 金额选择性序列化指根据传入的金额值,选择是否进行序列化,以达到减少数据传输的目的。在实…

    编程 2025-04-29
  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Python中将字符串转化为浮点数

    本文将介绍在Python中将字符串转化为浮点数的常用方法。在介绍方法之前,我们先来思考一下这个问题应该如何解决。 一、eval函数 在Python中,最简单、最常用的将字符串转化为…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29

发表回复

登录后才能评论