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?
Printable View
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.
This is what i have so far:
Problem is when i run it i get this error:
I thought ROUND_CEILING was supposed to get rid of needing to round?Code java:
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?
Code java:
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?Quote:
big.setScale(BigDecimal.ROUND_CEILING)
Have you set the rounding first?Quote:
ArithmeticException: Rounding necessary
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?
the output = 2
Then the code was executing: setScale(2)
Try calling setScale(9999) and see what happens.
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.Quote:
It does not continue