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?
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.
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?
Look at using the BigDecimal class.
If you don't understand my answer, don't ignore it, ask a question.
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:
I thought ROUND_CEILING was supposed to get rid of needing to round?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
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.
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
What do you expect that to do?big.setScale(BigDecimal.ROUND_CEILING)
Have you set the rounding first?ArithmeticException: Rounding necessary
If you don't understand my answer, don't ignore it, ask a question.
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.
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.
the output = 2
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.
It enters a lot of 0's. It does not continue answering the equation tho. Why is that?
I don't know why the program stops executing.It does not continue
If you don't understand my answer, don't ignore it, ask a question.