package AA;
import java.util.Scanner;
public class AA {
public static void main(String[] args) {
Scanner W=new Scanner(System.in);
double a, b,c,d,e,f,g,h;
a=W.nextDouble();
b=(int)a/10;
c=(int)(a-b*10)/5;
d=(int)(a-10*b-5*c)/1;
e=(double)(a-10*b-5*c-d)/0.5;
f=(double)(a-10*b-5*c-d-e*0.5)/0.1;
g=(double)(a-10*b-5*c-d-e*0.5-f*0.1)/0.02;
h=(double)(a-10*b-5*c-d-e*0.5-f*0.1-g*0.02)/0.01;
System.out.printf("%.0f 张十元\n",b);
System.out.printf("%.0f 张五元\n",c);
System.out.printf("%.0f 张一元\n",d);
System.out.printf("%.2f 张五角\n",e);
System.out.printf("%.2f 张一角\n",f);
System.out.printf("%.2f 张贰分\n",g);
System.out.printf("%.2f 张壹份\n",h);
}
}
输入 47.63
结果
4 张十元
1 张五元
2 张一元
1.26 张五角 //为什么输出的是1.26不是1
0 张一角 //为什么输出为0
0.00 张贰分
0.00 张壹份
是不是数据类型转换错误了?
求解惑,感激不尽
给你个参考
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
long aLong = Math.round((a * 100));
long b = aLong / 1000;
long c = aLong % 1000 / 500;
long d = aLong % 500 / 100;
long f = aLong % 100 / 50;
long j = aLong % 50 / 10;
long h = aLong % 10 / 2;
long i = aLong % 2;
System.out.println(b + " 张十元");
System.out.println(c + " 张五元");
System.out.println(d + " 张一元");
System.out.println(f + " 个五角");
System.out.println(j + " 个一角");
System.out.println(h + " 个贰分");
System.out.print(i + " 个壹分");