「java金额函数」java中金额类型
本篇文章给大家谈谈java金额函数,以及java中金额类型对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java编程计算付款和找零
- 2、java实现金额转换,阿拉伯数字的金额转换成中国传统的形式
- 3、java 求助 要求如下
- 4、java 输出应给顾客找零金额的各种面额人民币张数.在主函数中输入商品价格和顾客付款,调用函数得结果.
java编程计算付款和找零
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Change {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("请输入价格:");
double m = Double.parseDouble(getInputLine());
System.out.println("请付款金额:");
double c = Double.parseDouble(getInputLine());
change(m, c);
}
public static String getInputLine() {
InputStream inputStream = System.in;
ListByte cmdBuffer = new ArrayListByte();
int t;
try {
while (true) {
t = inputStream.read();
if (t == '\n') {
byte[] bs = new byte[cmdBuffer.size()];
for (int i = 0; i cmdBuffer.size(); i++) {
bs[i] = cmdBuffer.get(i);
}
return new String(bs).trim();
} else {
cmdBuffer.add((byte) t);
if (cmdBuffer.size() 1024) {
return null;
}
}
}
} catch (IOException e) {
return null;
}
}
public static double[] moneys = { 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100 };
/**
* @param m
* 价格
* @param c
* 付款
*/
public static void change(double m, double c) {
double z = c - m;
if (z 0) {
System.out.println("付款不足!");
} else if (z == 0) {
System.out.println("不需要找零。");
} else {
System.out.println("找零共:" + z + "元");
System.out.println("其中:");
for (int i = moneys.length - 1; i = 0; i--) {
double money = moneys[i];
int count = 0;
while ((z - money) = 0) {
z -= money;
count++;
}
if (count 0) {
System.out.println(money + "元:" + count + "张");
}
}
}
}
}
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方法是具体的调用。
java 求助 要求如下
类BankAccount
public class BankAccount {
private String m_sName;
private double m_dBalance;
private double m_dInterestRate;
public BankAccount(){}//无参数的构造函数
public BankAccount(String m_sName,double m_dBalance,double m_dInterestRate){
this.m_sName=m_sName;
this.m_dBalance=m_dBalance;
this.m_dInterestRate=m_dInterestRate;
}
public double getBalance() {//获取当前存款
return m_dBalance;
}
public double getInterestRate() {//获取当前利率
return m_dInterestRate;
}
public void setInterestRate(double interestRate) {//设置利率
m_dInterestRate = interestRate;
System.out.println("当前利率更改为"+interestRate);
}
public void srintAccountMeg(){//打印当前信息
System.out.println("账号名:"+this.m_sName);
System.out.println("存款余额:"+this.m_dBalance);
System.out.println("当前利率:"+this.m_dInterestRate);
}
public void saveMoney(double money){//存款
this.m_dBalance=this.m_dBalance+money;
System.out.println("存入"+money);
System.out.println("当前余额"+this.m_dBalance);
}
public void getMoney(double money){//取款
if(this.m_dBalance=money){
this.m_dBalance=this.m_dBalance-money;
System.out.println("取出"+money);
System.out.println("当前余额"+this.m_dBalance);
}else{
System.out.println("余额不足");
}
}
public double caculateInterest(int days){//计算多少天后的总存款
int year =days/365;
for(int i=1;i=year;i++){
this.m_dBalance=this.m_dBalance+this.m_dBalance*this.m_dInterestRate;
}
return this.m_dBalance;
}
}
另一个类SimpleAccount
import java.math.BigDecimal;
public class SimpleAccount {
public static void main(String[] args) {
BankAccount p1 = new BankAccount("张三",1000,0.03);
p1.srintAccountMeg();//打印当前信息
p1.saveMoney(3000);//存3000
p1.srintAccountMeg();//打印当前信息
p1.getMoney(500);
p1.srintAccountMeg();//打印当前信息
int year=10;
double totalMoney = p1.caculateInterest(year*365);
BigDecimal bd1= new BigDecimal(totalMoney);//这边的BigDecimal 是为了保留存款小数点后2位
BigDecimal totalMoney4=bd1.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("修改利率前--"+year+"年后的存款:"+totalMoney4);
p1.setInterestRate(0.3);
double totalMoney2 = p1.caculateInterest(year*365);
BigDecimal bd2= new BigDecimal(totalMoney2);
BigDecimal totalMoney3=bd2.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("修改利率后--"+year+"年后的存款:"+totalMoney3);
}
}
java 输出应给顾客找零金额的各种面额人民币张数.在主函数中输入商品价格和顾客付款,调用函数得结果.
人民币的设计就是按照最大面值最小张数生成的概念设计的。
所以每次你只要用“差/当前可选最大面值”然后递归去算就可以了。
比如差8块、直接8/5 然后余3再 3/2余 1然后1/1余0,递归结束。就可以了。
java金额函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java中金额类型、java金额函数的信息别忘了在本站进行查找喔。
发布于:2022-12-09,除非注明,否则均为
原创文章,转载请注明出处。