-3

I call the following method to convert the inputs as indicated in the body :

public static double convertFromHectareAreCentiareToHectare(int hectare, int are, int centiare){
            double d;
            DecimalFormat df = new DecimalFormat("######.####");
            d = Double.valueOf(df.format((hectare*10000+ are * 100 + centiare)/10000));
            return d;
        }

    }  

When I call this method with :

convertFromHectareAreCentiareToHectare(1, 90, 90)

I get : 1.0

But I should normally get 1.9090
What is the problem with my code ?

EDIT :

After changing 10000 to 10000.0, I got the following exception :

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1,909"
        at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
        at java.lang.Double.valueOf(Unknown Source)
        at ma.ancfcc.Utilities.Convert.convertFromHectareAreCentiareToHectare(Convert.java:21)
        at ma.ancfcc.Utilities.Convert.main(Convert.java:8)
mounaim
  • 1,132
  • 7
  • 29
  • 56
  • 2
    `int / int` doesn't produce `double`. – devnull Apr 04 '14 at 10:47
  • 2
    There is no such thing as 'a floating number with an exact number of decimals'. If you want decimal places, use a decimal radix. Floating-point cannot do it. Return the String produced by the formatting operation, or a BigDecimal produced from that. – user207421 Apr 04 '14 at 10:49
  • so rather than writing 10000 I should just try 10000.0 this is it @devnull ? – mounaim Apr 04 '14 at 10:50
  • not working @devnull :) even If I change the type of the parameters to double I keep getting the result 1.0 ! – mounaim Apr 04 '14 at 10:54
  • Did you read the comment chain? If not, do it again. – devnull Apr 04 '14 at 10:55
  • @mohnmain Nobody said to change the parameters to double. You're ignoring everything you've been told here. So why post? – user207421 Apr 04 '14 at 11:00
  • I'm going to reiterate what EJP said, because apparently it's difficult to read. There is no such thing as 'a floating number with an exact number of decimals'. The whole idea behind floating point arithmetic (as opposed to fixed poi nt arithmetic) is that you have a fixed amount of space that can hold a very large range of numbers. The trade off is that you get less accurate digits. A float is about seven and a double is about sixteen if memory serves. Also irrational numbers like Pi, e and sqrt(2) can never ever be represented with an exact number of decimals, for obvious reasons. – Mikkel Løkke Apr 04 '14 at 11:53
  • @MikkelLøkke The trade-off isn't that you get 'less accurate digits', whatever that means. The trade-off is that you get 53 *bits* of precision, *in binary radix,* which is *incommensurable* with decimal radix, i.e. with *any* specific number of decimal digits other than those that agree with the negative powers of two. – user207421 Apr 05 '14 at 00:34
  • @EJP, Yeah, just like I said. :) – Mikkel Løkke Apr 05 '14 at 13:48

1 Answers1

1

double or float number can only be formatted to have a given number of digit after decimal separator when it is converted to a String. You can not enforce decimal precision of the numbers, just their String representation.
P.S. don't use double to handle fixed precision calculations. Convert the numbers to long and handle whole numbers of smallest units.
I.e. if you want to handle weight with milligram precision use milligram as a unit of calculation, so that 1.5kg will become 1,500,000 (milligrams).

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19