开发者

Can't get java to round 24.9999 properly, rounds to 8.?

开发者 https://www.devze.com 2023-04-01 02:39 出处:网络
I\'m learning java and have a bit of code I am trying to write that should round 24.9999999 to 25. Instead, it goes to 8.

I'm learning java and have a bit of code I am trying to write that should round 24.9999999 to 25. Instead, it goes to 8.

import java.io.*;
import java.util.*;
public class RadiusOfCircle
{
    public static void main(String args[])
    {
        Scanne开发者_运维问答r kbInput = new Scanner(System.in);
        System.out.print("What is the area? _");
        double area = kbInput.nextInt();
        System.out.println("Radius of your circle is " + Math.sqrt( area / Math.PI));

        double radius = Math.sqrt( area / Math.PI);
        System.out.println("\nChecking your work now...\n   area = pi*(r^2)\n   " + area + " = 3.14 * (" + radius + ")^2");
        double radiusSqrd = Math.pow(radius, 2);
        System.out.println("   " + area + " = 3.14 * " + radiusSqrd);
        System.out.println("   " + area + " = " + Math.PI * radiusSqrd);
        System.out.println(area + " = " + (Math.round(radiusSqrd)));
        System.out.println("Are the two values the same? \nIf yes, your code is correct! \nIf no, try again!");
    }
}

Also, when it asks for keyboard input of what the area is, I put in 25.

This is the output:

What is the area? _25
Radius of your circle is 2.8209479177387813

Checking your work now...
   area = pi*(r^2)
   25.0 = 3.14 * (2.8209479177387813)^2
   25.0 = 3.14 * 7.957747154594766
   25.0 = 24.999999999999996
25.0 = 8
Are the two values the same? 
If yes, your code is correct! 
If no, try again!


You're rounding radiusSquared only, rather than Math.PI * radiusSquared. Fixing that should get the result you expect.


System.out.println("   " + area + " = " + Math.PI * radiusSqrd);
System.out.println(area + " = " + (Math.round(radiusSqrd)));

Shouldn't that be:

double value = Math.PI * radiusSqrd;
System.out.println("   " + area + " = " +value );
System.out.println(area + " = " + (Math.round(value )));


You omitted to multiply by PI:

System.out.println(area + " = " + Math.round(Math.PI * radiusSqrd));

Executing this gives the expected result.


float and double were designed for engineering problems, which have large positive powers of 10.. they cannot express negative powers of 10 accurately. In such cases use BigDecimal.

Run this simple code from Joshua Bloch's book Effective Java to get a sense of the extend of inaccuracy when dealing with negative powers and using double to store them. The answer should acutally be zero, but turns out to be something else entirely!

double funds = 1.00; 
int itemsBought = 0; 
for (double price = .10; funds >= price; price += .10) { 
  funds -= price; 
  itemsBought++; 
} 
System.out.println(itemsBought + ” items bought.”); 
System.out.println(“Change: $” + funds); 
} 


If you want "accurate" math in Java, you should use the BigDecimal class, rather than either of the built-in floating point primitive types. double and float are always going to have issues like this, due to the nature of floating point arithmetic.

Please note that the recommended constructor for BigDecimal uses a String, not any of the numeric types. Since you are getting input from the console, this should be easy to implement.


You can use something like this:

    double d = 10.938;

    BigDecimal bd = new BigDecimal(d);
    bd = bd.setScale(0,BigDecimal.ROUND_HALF_UP);
    System.out.println(bd);

    bd = new BigDecimal(d);
    bd = bd.setScale(1,BigDecimal.ROUND_HALF_UP);        
    System.out.println(bd);

    bd = new BigDecimal(d);        
    bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
    System.out.println(bd);

or like this:

    double d = 10.938;

    DecimalFormat decimalFormat = new DecimalFormat("#");        
    System.out.println(decimalFormat.format(d));

    decimalFormat = new DecimalFormat("#.#");
    System.out.println(decimalFormat.format(d));

    decimalFormat = new DecimalFormat("#.##");
    System.out.println(decimalFormat.format(d));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号