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

1. Member 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 numb;

lowerLimit = input.nextDouble();

upperLimit = input.nextDouble();

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

}

}```
Last edited by mackiver; 10-24-2011 at 04:19 AM.  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

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

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.

Java 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.
Last edited by pbrockway2; 10-24-2011 at 05:00 AM.  Reply With Quote

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.  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•