回答: 一般使用 java.math.BigDecimal 类来处理需要高精度的计算
创新互联建站专注于江夏企业网站建设,响应式网站开发,成都商城网站开发。江夏网站建设公司,为江夏等地区提供建站服务。全流程按需定制开发,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
原因: 1.浮点数(double,flot)因为精度问题, 容易出现不精确的计算结果
2.浮点数范围比较小
BigDecimal 范围大, 并且比他们精确
参考代码
import java.math.BigDecimal;
public class Demo {
public static void main(String[] args) {
//浮点数,存在精度问题
double d1 = 0.19*6;
System.out.println(d1);//1.1400000000000001
//采用BigDecimal 来解决精度问题
BigDecimal bd1 = new BigDecimal("0.19");
BigDecimal bd2 = new BigDecimal("6");
BigDecimal bd3 = bd1.multiply(bd2);
System.out.println(bd3);//1.14
}
}
输出
1.1400000000000001
1.14
/**
金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥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("错误");
}
}
}
public class Money {
public static void main(String[] args) {
// 所有金额以分为单位
int money = 146;
// 所有钞票面以分为单位,从大到小排列,10000是100块,20是2角
int[] rmbs = { 10000, 5000, 2000, 1000, 500, 100, 50, 20, 10, 5, 2, 1 };
// rest是剩余的钱,刚开始为所求的钱,随便选钱rest越变越小,直到为0
int rest = money;
for (int rmb : rmbs) {
// num为钞票个数
int num = rest / rmb;
if (num 0) {
rest = rest - rmb * num;
System.out.println("钞票面额(分):" + rmb + " 个数:" + num + " 还差多少钱(分):" + rest);
// 钱凑齐了,程序结束
if (rest == 0) {
System.exit(1);
}
} // end if num
} // end for rmb
}
}
public static void main(String[] args) {
double amount = 1200;// 消费金额
boolean isTeacherOrOld = true; // 是否有教师资格证或者老人标识
BigDecimal discountEightFive = new BigDecimal("0.85");// 85折
BigDecimal discountNine = new BigDecimal("0.9");// 95折
BigDecimal discountNineFive = new BigDecimal("0.95");// 95折
BigDecimal bdAmount = new BigDecimal(Double.toString(amount));
BigDecimal calcAmount = new BigDecimal("0.00");
if (amount = 2000) {
calcAmount = bdAmount.multiply(discountEightFive);
} else if (amount 1000 amount 2000) {
calcAmount = bdAmount.multiply(discountNine);
} else {
calcAmount = new BigDecimal(Double.toString(amount));
}
if (isTeacherOrOld amount = 1000) {
System.out.println("消费金额:" + calcAmount.multiply(discountNineFive).toString());
} else {
System.out.println("消费金额:" + calcAmount.toString());
}
}