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!

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 );

}

}

Re: Help decimal number { float or double??} HELP!

Quote:

i want these variables to be represented in decimal numbers rounded to the 6th place.

Be careful! If you start rounding *deltaX* it must become **less** accurate. And, in that case, you ought to call the method "quasi-" Simpson, since such rounding is not what the 18th century mathematician had in mind. (or Kepler before him)

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.

Code:

`System.out.printf("By Simpson's rule, the final answer is: %.6f" , answer );`

For details see the printf() API docs (which includes a link to the format string syntax). There are also usage examples in the Formatting Numeric Print Output page of Oracle's Tutorial.

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.

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.