使用java.util.Scanner在测试中输入两倍的1234567889,给出1.23456789E8的输出

使用java.util.Scanner在测试中输入两倍的1234567889,给出1.23456789E8的输出,java,Java,使用java.util.Scanner在测试中输入两倍的1234567889,给出1.23456789E8的输出。我在控制台中输入它来测试输出,它应该给我123456789.0的输出。我很困惑,我一直在寻找为什么会发生这种情况,只是找不到解决办法 import java.util.Date; import java.util.Scanner; /** * Created by Jeremy Roth on 10/6/2016 PRG420 Java I. * What we need

使用java.util.Scanner在测试中输入两倍的1234567889,给出1.23456789E8的输出。我在控制台中输入它来测试输出,它应该给我123456789.0的输出。我很困惑,我一直在寻找为什么会发生这种情况,只是找不到解决办法

import java.util.Date;
import java.util.Scanner;



/**
 * Created by Jeremy Roth on 10/6/2016  PRG420 Java I.
 * What we need calculated:
 * salary (constant at $55,000.00 per year)
 * totalSales  (total sales for the year)
 * commissionPercentRate from totalSales (percentage of earnings from totalSales)
 * salary + yearlyCommission
 * output: yearlyCommission
 * output: salary + yearlyCommission = earningYearTotal (earnings for the year total)
 *
 * This is just a simple assignment for console output but my question/goal is what further examples or changes might
 * be necessary to convert this into GUI output later in the course?
 */

public class Main {

    private static final double salary = 55000.00;  //my attempt and making this immutable not sure if I'm right
    public static final double commissionPercentRate = 0.12; //my attempt and making this immutable not sure
    public double totalSales = 0; //console input by user
    public double yearlyCommission = 0;
    //private static DecimalFormat df2 = new DecimalFormat("###,###,###.##");


    // (2) The instance variables are visible for all methods, constructors and block in the class. Normally,
    // it is recommended to make these variables private (access level). However, visibility for
    // subclasses can be given for these variables with the use of access modifiers.



    public static void main(String[] args) {

        Date date = new Date();

        System.out.println("Welcome to the Sales Commission Calculator!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }

        System.out.println("Today is :" + date.toString());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }

        System.out.println("Make sure you have your sales documentation ready!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        System.out.println("You will need your information handy to calculate your commissions!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the amount of your sales for the year: ");
        double number = scanner.nextDouble();
        System.out.println("The amount you entered in sales for the year is: $" + number);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        System.out.println("Your yearly salary is: $" + salary);



    }
}
控制台输出:

Welcome to the Sales Commission Calculator!
Today is :Thu Oct 06 20:47:22 CDT 2016
Make sure you have your sales documentation ready!
You will need your information handy to calculate your commissions!
Enter the amount of your sales for the year: 
123456789
The amount you entered in sales for the year is: $1.23456789E8
Your yearly salary is: $55000.0

Process finished with exit code 0

虽然我不建议使用double原语类型来表示money BigDecimal、int或long,但您可以使用System.out.printf来避免看到指数形式:

System.out.printf("The amount you entered in sales for the year is: $%.2f", number);

1.23456789E8只是123456789的科学符号

1.23456789E8=1.23456789*10^8=123456789

如果您只是想在输出中显示为123456789,可以使用printf而不是println对其进行格式化:

System.out.printf您在本年度的销售额中输入的金额为:$%.0f\n,数字

NPE在邮件中回答类似问题的功劳:


它所做的只是将小数点向左移动8个空格。有什么问题吗?请尝试System.out.Printf您在本年度的销售额中输入的金额为:$%.2f%n,数字;我要试一试!谢谢这是一个解决方案,它打印两位小数。我知道这必须很简单!我在这么简单的事情上想得太多了。再次感谢!输入本年度的销售额:123456789您在本年度的销售额中输入的金额是:$123456789.00谢谢!我的下一步是使用BigDecimal编写第二个版本,你说得很对。这是一个比我想象的更简单的错误。