Results 1 to 4 of 4
 10242011, 05:14 AM #1Member
 Join Date
 Oct 2011
 Posts
 1
 Rep Power
 0
Help decimal number { float or double??} HELP!
Please if u could run the program, and when asked please use 1 for lower limit and 3 for upper limit, and finally 46 for n.
I have assigned deltaX = ( ( upperLimit  lowerLimit) / subInt ) and then multiplied it by 1/3, which is the value of numb which does not give me a decimal number, same goes for variable answer, i want these variables to be represented in decimal numbers rounded to the 6th place. Any help would be highly appreciated. Thanks!
Java Code:import java.util.Scanner; import java.math.BigDecimal; public class Simps { public static void main ( String args[] ) { Scanner input = new Scanner(System.in); double lowerLimit, upperLimit, subInt; double deltaX; double deltaN = 1/3; double answer; double numb; System.out.println("Please enter your lower limit (a): \n"); lowerLimit = input.nextDouble(); System.out.println("Please enter your upper limit (b): \n "); upperLimit = input.nextDouble(); System.out.println("Please enter your subinterval (n): \n "); subInt = input.nextDouble(); deltaX = ( ( upperLimit  lowerLimit) / subInt ); System.out.printf(" Your h is : %f \n" , deltaX); numb = deltaN * deltaX; System.out.printf(" h will be multiplied by 1/3: %f \n\n" , numb); answer = (float) (( 1 + (23/6) + (46/25) + (46/13) + (46/27) + (23/7) + (46/29) + (46/15) + (46/31) + (23/8) + (46/33) + (46/17) + (46/35) + (23/9) + (46/37) + (46/19) + (46/39) + (23/16) + (46/41) + (46/21) +(46/43) + (23/11) + (46/45) + (2) + (46/47) + (23/12) + (46/49) + (46/25) + (46/51) + (23/13) + (46/53) + (46/27) + (46/55) + (23/14) + (46/57) + (46/29) +(46/59) + (23/15) + (46/61) + (46/31) + (46/63) + (23/16) + (46/65) + (46/33) + (46/67) + (23/17) + (1/3) ) * numb); System.out.printf("By Simpson's rule, the final answer is: %f" , answer ); } }
Last edited by mackiver; 10242011 at 05:19 AM.
 10242011, 05:57 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
Re: Help decimal number { float or double??} HELP!
i want these variables to be represented in decimal numbers rounded to the 6th place.
Displaying numeric quantities with a finite (and smallish) number of decimal places in its String representation, as you might want to do with the answer is another matter: that's not rounding, it's formatting. The printf() method is designed to do just that.
Java Code:System.out.printf("By Simpson's rule, the final answer is: %.6f" , answer );
Last edited by pbrockway2; 10242011 at 06:00 AM.
 10242011, 06:01 AM #3
Re: Help decimal number { float or double??} HELP!
If you want a floating point number to be displayed in a certain format then you can use the DecimalFormat class. Note that this class does not alter the value of the number it simply generates a String representation according to your parameters that can be displayed to the user.
 10242011, 06:06 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
Re: Help decimal number { float or double??} HELP!
Another matter is that you should use double not float. Both are floating point number types, but float is not as precise as double. Casting the expression to float (before assigning it to a double variable) will just introduce some more inaccuracy  although probably out past the 6th decimal place of the answer in its String form.
Similar Threads

What is the difference between float and double?
By Nosrettap in forum New To JavaReplies: 5Last Post: 09232014, 01:20 PM 
double and float value in java
By arvindmer in forum New To JavaReplies: 6Last Post: 05282009, 05:51 PM 
Float vs. Double
By javanub in forum New To JavaReplies: 1Last Post: 11232008, 01:11 PM 
Float and double to binary
By rosh72851 in forum Advanced JavaReplies: 3Last Post: 09242008, 07:29 AM 
Put float value to double
By chamalsl in forum New To JavaReplies: 3Last Post: 09202008, 05:52 AM
Bookmarks