在Java中分离小数值

huangapple go评论71阅读模式
英文:

Separating decimal value in Java

问题

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the number of quarters: ");
    double qs = sc.nextDouble();
    System.out.print("Enter the number of dimes: ");
    double ds = sc.nextDouble();
    System.out.print("Enter the number of nickels: ");
    double ns = sc.nextDouble();
    System.out.print("Enter the number of pennies: ");
    double ps = sc.nextDouble();
    System.out.print("\n");

    double qV = 0.25;
    double dV = 0.10;
    double nV = 0.05;
    double pV = 0.01;
    double tQ = qs * qV, tD = ds * dV, tN = ns * nV, tP = ps * pV;
    double totalV = tQ + tD + tN + tP;
    int dollars = (int) totalV;
    double cent = (totalV - dollars) * 100;
    int cents = (int) cent;

    int tqs = (int) qs;
    int tds = (int) ds;
    int tns = (int) ns;
    int tps = (int) ps;

    System.out.println("You entered " + tqs + " quarters.");
    System.out.println("You entered " + tds + " dimes.");
    System.out.println("You entered " + tns + " nickels.");
    System.out.println("You entered " + tps + " pennies.");
    System.out.print("\n");

    System.out.println("Your total is " + dollars + " dollars and " + cents + " cents.");
}
英文:

So I was building a program where I take different coins and print out the value into dollars and cents. Since I needed each to be a whole number I did some coding to make it so. My problem is that on certain instances, when running the program, my cents value comes out wrong. EVERYTHING else is correct. I'm assuming this is something to do with converting to an integer and its causing it not to round when certain user input is given. For example, when I enter 10 quarters, 3 dimes, 6 nickels, and 2 pennies, it comes out with 11 cents at the end. I can not figure out the error here.

My full code is below. Can anyone let me know what I did wrong?

    public static void main (String[]args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of quarters: ");
double qs=sc.nextDouble();
System.out.print("Enter the number of dimes: ");
double ds=sc.nextDouble();
System.out.print("Enter the number of nickels: ");
double ns=sc.nextDouble();
System.out.print("Enter the number of pennies: ");
double ps=sc.nextDouble();
System.out.print("\n");
double qV=0.25;
double dV=0.10;
double nV=0.05;
double pV=0.01;
double tQ=qs*qV, tD=ds*dV, tN=ns*nV, tP=ps*pV;
double totalV=tQ+tD+tN+tP;
int dollars=(int)totalV;
double cent=(totalV-dollars)*100;
int cents=(int)cent;
int tqs=(int)qs;
int tds=(int)ds;
int tns=(int)ns;
int tps=(int)ps;
System.out.println("You entered "+tqs+" quarters.");
System.out.println("You entered "+tds+" dimes.");
System.out.println("You entered "+tns+" nickels.");
System.out.println("You entered "+tps+" pennies.");
System.out.print("\n");
System.out.println("Your total is "+dollars+" dollars and "+cents+" cents.");
}

答案1

得分: 0

你好,

获得你想要的正确方式是使用Math.round()方法,如下所示:

int cents = (int) Math.round(cent);

对于你的美元值,我会采用相同的方法。你可以阅读更多关于IEEE 754计算的信息,这里有一个很好的解释,说明了底层发生的情况 - https://stackoverflow.com/questions/40300041/is-there-any-ieee-754-standard-implementations-for-java-floating-point-primitive

英文:

Good day,

The right way to get what you wanted is to use Math.round() method as such:

int cents= (int) Math.round(cent);

I would apply the same approach for your dollars value. You may read more about IEEE 754 calculations, here is a good is a good explanation on what is going on under the hood - https://stackoverflow.com/questions/40300041/is-there-any-ieee-754-standard-implementations-for-java-floating-point-primitive

答案2

得分: 0

不要使用 double

硬币的数量始终是整数。使用 int 来表示整数。

如果你用分来计算总额,金额可以是一个整数。使用 int

System.out.print("输入25美分硬币的数量:");
int qs = sc.nextInt();
// … 等等 …

int total = 25 * qs + 10 * ds + 5 * ns + ps;

现在,美元的数量是总分数除以100得到的商,而分的数量是将总分数除以100得到的余数。

int dollars = total / 100;
int cents = total % 100;
System.out.println("您的总额为 " + dollars + " 美元和 "
                   + cents + " 分。");

这比进行转换要简单得多,而且计算始终是准确的。

英文:

Don't use double at all.

The number of coins is always a whole number. Use int to represent a whole number.

An amount of money can be a whole number if you count the total in cents. Use int.

System.out.print("Enter the number of quarters: ");
int qs=sc.nextInt();
… etc …
int total = 25*qs + 10*ds + 5*ns + ps;

Now, the number of dollars is the total number of cents divided by 100, and the number of cents is the remainder when dividing the total by 100.

int dollars = total / 100;
int cents = total % 100;
System.out.println("Your total is " + dollars + " dollars and "
+ cents + " cents.");

This is a lot simpler than having to do the conversions, and the artithmetic is always exact.

huangapple
  • 本文由 发表于 2020年9月7日 09:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63770295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定