Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Double rounding

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Double rounding

    Hi .. I would like to round off a number in a method and return the rounded number. I tried many different ways but I always got the same error message:

    Matematik.java:70: error: incompatible types
    return df.format(volym);
    ^
    required: double
    found: String
    1 error

    import java.text.DecimalFormat;
     
    	public static double volume (int r, int h)
    	{
    	DecimalFormat df = new DecimalFormat("#.##");
     
    	double volym=PI*r*r*h;
    	return df.format(volym);
    	}


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Double rounding

    Your DecimalFormat object returns a String object. Use the Double class to parse the String and return a double value.

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    krillov (June 10th, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Double rounding

    I´m not sure if I did right but I did like this:

    	public static double volume (int r, int h)
    	{
    	DecimalFormat df = new DecimalFormat("#.##");
    	double volym=PI*r*r*h;
    	return Double.valueOf(df.format(volym));
    	}

    and got the error message like this:

    --------------------Configuration: <Default>--------------------
    Klotets volym är: 113,04

    Cylinderns volym är: Exception in thread "main" java.lang.NumberFormatException: For input string: "28,26"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241)
    at java.lang.Double.valueOf(Double.java:504)
    at Matematik.volume(Matematik.java:69)
    at Matematik.main(Matematik.java:39)

    Process completed.

    Do you know maybe what I did wrong? :/

  5. #4
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Double rounding

    For some odd reason it's returning a comma rather than the period. Try using this as your format "###.##"

    Not too sure why it is returning a comma there though.

  6. The Following User Says Thank You to Parranoia For This Useful Post:

    krillov (June 10th, 2012)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Double rounding

    Some locale's swap . and , for floating point numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    krillov (June 10th, 2012)

  9. #6
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Double rounding

    Thanks for you both. I changed like this but it isn´t working correctly I got this result 28.0. It should be 28.26. And if I change the value of "h" in the main then I get this message:

    Cylinderns volym är: Exception in thread "main" java.lang.NumberFormatException: For input string: "1*98"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241)
    at java.lang.Double.valueOf(Double.java:504)
    at Matematik.volume(Matematik.java:69)
    at Matematik.main(Matematik.java:39)

    (the result should be 197.82)

    I just tried to return String instead of double and it works that way, but of course I must return double...
    Last edited by krillov; June 10th, 2012 at 04:22 PM.

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Double rounding

    Can you make a small simple program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #8
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Double rounding

    What is your value for PI? Also, the data type. I just tested this code and it works just fine. My only guess now is that your PI variable is set wrong :/

    public static double volume (int r, int h)
    {
    	DecimalFormat df = new DecimalFormat("#.##");
     
    	double volym=3.14*r*r*h;
    return Double.parseDouble(df.format(volym));
    }

  12. #9
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Double rounding

    I defined PI like this: public static final double PI = 3.14;

    It´s very strange. I made a simple program and copied your code, but I got the same fail.

    import java.text.DecimalFormat;
     
    public class Matte {
     
    public static void main (String[] args)
    {
     
    int r=3;
    int h=5;
     
     System.out.print(volume(r,h));
    }
     
     
    public static double volume (int r, int h)
    {
    	DecimalFormat df = new DecimalFormat("#.##");
     
    	double volym=3.14*r*r*h;
    return Double.parseDouble(df.format(volym));
    }
    }

  13. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Double rounding

    Hello krillov!
    I tried your code and it gives me the same error. It's probably what Norm says in post#5. A "sloppy" way to fix this is by replacing the "," with a "." to the String which is returned by the DecimalFormat format method and then parse it to a double. The String class has methods for that.
    Hope this helps.

  14. #11
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Double rounding

    Thanks, it worked. I tried in a smaller program like this:

    import java.text.DecimalFormat;
     
    public class Matte {
     
    	public static final double PI = 3.14;
     
    public static void main (String[] args)
    {
     
    int r=3;
    int h=7;
     
     System.out.print(volume(r,h));
    }
     
     
    public static double volume (int r, int h)
    {
    	DecimalFormat df = new DecimalFormat("#.##");
     
    	double volym=PI*r*r*h;
    	String volym2=df.format(volym);
    	volym2=(replace1(volym2));
    	return Double.parseDouble(volym2);
    }
     
     
    public static String replace1(String volym2)
    {
     	return volym2.replaceAll("," , ".");
    }
    }

Similar Threads

  1. [SOLVED] Rounding Decimal Numbers Question
    By Nuggets in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 04:12 PM
  2. Question about rounding
    By CoolGuy134 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 27th, 2011, 04:30 PM
  3. Rounding the numbers
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 12:45 AM
  4. Rounding an int. Help please
    By Akim827 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2010, 12:36 AM
  5. Rounding screwed up?
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2010, 01:43 PM