This question already has answers here:
How do I print a double value without scientific notation using Java?
                                
                                    (14个回答)
                                
                        
                2年前关闭。
            
        

    double energy, mass;
    double speedOfLight = 299792458.0;
    // Get mass
    System.out.print("Mass? ");
    mass = keyboard.nextDouble();
    // Calculate energy
    energy = mass*Math.pow(speedOfLight, 2);
    // Round to 1 decimal place
    energy = Math.round(energy * 10.0) / 10.0;
    System.out.printf("The energy is " +energy+ " Joules");
    // Close scanner
    keyboard.close();


它应返回时返回“能量为8.987551787368176E16焦耳”,“应返回能量为89875517873681760.00.0焦耳”

最佳答案

Math.round正常工作,返回最接近的整数(但输出在scientific notation中)。问题出在号码的打印上。您可以使用printfformat specifiers以所需的格式打印数字:

System.out.printf("The energy is %.1f Joules", energy);

09-08 03:40