How can add numbers with decimals?

greenspun.com : LUSENET : Grundlfggende Programmering, F2000 : One Thread

Dear Niels, I got the program Sum2(3-31) to work with integers. I would like to execute the program (modified) with numbers with decimals. Changing 'Integer.parseInt' to 'Double.parsedouble' did not help. Thanks Joan.

-- Joan Campbell (joan@itu.dk), February 27, 2000

Answers

Hi,

the trick to parse floats is as follows:

double d = Double.valueOf(args[0]).doubleValue();
that will convert the first commandline argument (indexed by 0) into a double if possible. An error is reported if the commandline argument can't be converted into a float.

You can actually make your own parseDouble method as follows:

  public static double parseDouble(String d) {
    return Double.valueOf(d).doubleValue();
  }

Happy Hacking

-- Niels Hallenberg (nh@itu.dk), February 28, 2000.


Moderation questions? read the FAQ