程序改变(舍入帮助)

时间:2016-04-29 15:51:10

标签: java rounding

我正在尝试计算程序中的镍币数量(不再有便士了)。因此,如果我输入1.44,它应该舍入到1.40,如果我有1.46,它应该舍入到1.50。请帮忙!

import java.util.Scanner;

public class MakingChange
{

    private static Scanner scanner;

    public static void main(String[] args)

    {
        scanner = new Scanner(System.in);
        double amount = 0;
        while (true) {
             try {
                 amount = Double.parseDouble(scanner.nextLine());
                 break; // will only get to here if input was a double
             } catch (NumberFormatException ignore)   {
                 System.out.println("INVALID\r\n$");
             }
         }

        //calculating amount of change in cents 
        int remainingAmount = (int)(amount * 100.00);
        //toonies
        int numberofToonies =   (int) (remainingAmount / 200.00);
        remainingAmount = remainingAmount % 200;
        //loonies   
        int numberofLoonies = (int) (remainingAmount / 100.00);
        remainingAmount = remainingAmount % 100;
        //quarters
        int numberofQuarters = (int)(remainingAmount / 25.00);
        remainingAmount = remainingAmount % 25;
        //dimes      
        int numberofDimes = (int)(remainingAmount / 10.00);
        remainingAmount = remainingAmount % 10;
        //nickels  
        int numberofNickels = (int)(remainingAmount / 5.00);
        remainingAmount = remainingAmount % 5;
        //rounded value
       numberofNickels=(int) (((amount -(numberofToonies * 2) - (numberofLoonies *1) - (numberofQuarters *0.25) - (numberofDimes * 0.10) - (numberofNickels * 0.05))+0.04)/0.05);

System.out.println(".*toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels +"$" );


        }

}

2 个答案:

答案 0 :(得分:2)

我建议将数字乘以10,再用Math.round(),然后再将其除以10。

1.46 * 10 = 14.6
Math.round(14.6) = 15
15 / 10 = 1.5

1.44 * 10 = 14.4
Math.round(14.4) = 14
14 / 10 = 1.4

这可以通过以下lambda实现:

d -> Math.round(d * 10) / 10.0

指定10.0而非10非常重要,因为Math.round()会返回long值,而您不希望整数除以浮动除法。

您可以在this ideone snippet上看到它。

答案 1 :(得分:0)

不要忘记询问用户的输入

public class MakingChange
{
    private static Scanner scanner;

    public static void main(String[] args) {

        scanner = new Scanner(System.in);
        System.out.println("Insert amount:");
        double amount = 0;
        while (true) {
            try {
                amount = Double.parseDouble(scanner.nextLine());
                break; // will only get to here if input was a double
            } catch (NumberFormatException ignore) {
                System.out.println("INVALID\r\n$");
            }
        }

        // calculating amount of change in cents
        int remainingAmount = (int) (amount * 100.00);
        // toonies
        int numberofToonies = (int) (remainingAmount / 200.00);
        remainingAmount = remainingAmount % 200;
        // loonies
        int numberofLoonies = (int) (remainingAmount / 100.00);
        remainingAmount = remainingAmount % 100;
        // quarters
        int numberofQuarters = (int) (remainingAmount / 25.00);
        remainingAmount = remainingAmount % 25;
        // dimes
        int numberofDimes = (int) (remainingAmount / 10.00);
        remainingAmount = remainingAmount % 10;

        // nickels
        int numberofNickels = 0;
        // if the remaining amount is exactly 5 cents
        if (remainingAmount == 5) {
            numberofNickels = 1;
        } else if (remainingAmount > 5)// round to higher value if remaining
                                        // value is greater than 5 cents e.g
                                        // 20.68
        {
            numberofDimes += 1;
        }

        System.out.println(".*toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:"
                + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels + "$");

    }
}