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 12 of 12

Thread: no ending after decimal point.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default no ending after decimal point.

    How do i get my program to keep going until the end is reach of a number? For example 22/7 = pie. How do i get the program to keep giving numbers after pie instead of stoping after 9 or 10 decimal places?


  2. #2
    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: no ending after decimal point.

    Look at using the BigDecimal class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no ending after decimal point.

    This is what i have so far:
     System.out.println((big.setScale(BigDecimal.ROUND_CEILING)));

    Problem is when i run it i get this error:
    Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
    	at java.math.BigDecimal.divideAndRound(BigDecimal.java:1452)
    	at java.math.BigDecimal.setScale(BigDecimal.java:2406)
    	at java.math.BigDecimal.setScale(BigDecimal.java:2449)
    	at pie.Pie.main(Pie.java:27)
    Java Result: 1
    I thought ROUND_CEILING was supposed to get rid of needing to round?

  4. #4
    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: no ending after decimal point.

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

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no ending after decimal point.

    import java.lang.Math;
     import java.math.BigDecimal;
     import java.text.NumberFormat;
     import java.util.*;
    /**
     *
     * 
     */
    public class Pie {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
        double num = 22;
        double num2 = 7;
       double total = num/num2;
       BigDecimal big = new BigDecimal(total);
     
     
     
            System.out.println((big.setScale(BigDecimal.ROUND_CEILING)));    // TODO code application logic here
        }
    }

    thats what i have that is giving the error

  6. #6
    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: no ending after decimal point.

    big.setScale(BigDecimal.ROUND_CEILING)
    What do you expect that to do?

    ArithmeticException: Rounding necessary
    Have you set the rounding first?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no ending after decimal point.

    I dont want it to round. I want the pie to continue forever until i shut down the program.

    I thought big.setscale(BigDecimal.ROUND_CIELING) would continue to count after the decimals with no set length.

  8. #8
    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: no ending after decimal point.

    Print out the value of BigDecimal.ROUND_CEILING to see its value.

    What happens when you leave the call to setScale() out?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no ending after decimal point.

    the output = 2

  10. #10
    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: no ending after decimal point.

    Then the code was executing: setScale(2)
    Try calling setScale(9999) and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no ending after decimal point.

    It enters a lot of 0's. It does not continue answering the equation tho. Why is that?

  12. #12
    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: no ending after decimal point.

    It does not continue
    I don't know why the program stops executing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  2. Program not ending correctly
    By alex067 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 15th, 2012, 07:54 AM
  3. Having trouble ending a game
    By wakaxwaka in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2012, 06:33 PM
  4. [SOLVED] JOptionPane preventing program from ending
    By RichTea in forum AWT / Java Swing
    Replies: 2
    Last Post: February 23rd, 2012, 07:42 AM
  5. program loops never ending
    By Ccogh05 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 24th, 2011, 01:42 AM