-2

I am extracting couple of values like 1234, 2456.00 etc from UI as string. When I try to parse this string to float, 1234 is becoming 1234.0 and when I tried to parse as double its throwing error. How can I solve this?

I am using selenium web driver and java. Below are few things I tried.

double Val=Double.parseDouble("SOQ");
double Val=(long)Double.parseDouble("SOQ");``
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Aish
  • 185
  • 1
  • 5
  • 16
  • 3
    `"SOQ"` is not a valid numeric string. – RealSkeptic Sep 28 '15 at 20:52
  • 1
    There are so many things wrong with that code.. Please neaten up. Are you just trying to convert a double to a long? or the other way around? – Nicholas Robinson Sep 28 '15 at 20:54
  • Ohh I am sorry..SOQ is the string which stores the text value that is extracted from UI. Its a typo while editing. – Aish Sep 29 '15 at 03:03
  • Sorry for the unclear question.From UI I am extracting the value 2456.00 as string. I parsed it using the code double X=Double.parseDouble(2456.00); After this the value of X is 2456.0 So what can I do to get the value as 2456.00 – Aish Sep 29 '15 at 04:29

4 Answers4

2

Your first problem is that "SOQ" is not a number.

Second, if you want create a number using a String, you can use parseDouble and give in a value that does not have a decimal point. Like so:

Double.parseDouble("1");

If you have a value saved as a long you do not have to do any conversions to save it as a double. This will compile and print 10.0:

long l = 10l;
double d = l;
System.out.println(d);

Finally, please read this Asking a good question

Community
  • 1
  • 1
Nicholas Robinson
  • 1,359
  • 1
  • 9
  • 20
2

The problem is you cannot parse non-numeric input as a Double.

For example:

Double.parseDouble("my text");
Double.parseDouble("alphanumeric1234");
Double.parseDouble("SOQ");

will cause errors.

but the following is valid:

Double.parseDouble("34");
Double.parseDouble("1234.00");
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
2

I think you mixed it up a bit when trying to figure out how to parse the numbers. So here is an overview:

// lets say you have two Strings, one with a simple int number and one floating point number
String anIntegerString = "1234";
String aDoubleString = "1234.123";

// you can parse the String with the integer value as  double 
double integerStringAsDoubleValue = Double.parseDouble(anIntegerString);
System.out.println("integer String as double value = " + integerStringAsDoubleValue);

// or you can parse the integer String as an int (of course)
int integerStringAsIntValue = Integer.parseInt(anIntegerString);
System.out.println("integer String as int value = " + integerStringAsIntValue);

 // if you have a String with some sort of floating point number, you can parse it as double
double doubleStringAsDoubleValue = Double.parseDouble(aDoubleString);
System.out.println("double String as double value = " + doubleStringAsDoubleValue);

// but you will not be able to parse an int as double
int doubleStringAsIntegerValue = Integer.parseInt(aDoubleString); // this throws a NumberFormatException because you are trying to force a double into an int - and java won't assume how to handle the digits after the . 
System.out.println("double String as int value = " + doubleStringAsIntegerValue);

This code would print out:

integer String as double value = 1234.0
integer String as int value = 1234
double String as double value = 1234.123

Exception in thread "main" java.lang.NumberFormatException: For input string: "1234.123"

Java will stop "parsing" the number right when it hits the . because an integer can never have a . and the same goes for any other non-numeric vales like "ABC", "123$", "one" ... A human may be able to read "123$" as a number, but Java won't make any assumptions on how to interpret the "$".

Furthermore: for float or double you can either provide a normal integer number or anything with a . somewhere, but no other character besides . is allowed (not even , or ; and not even a WHITESPACE)

EDIT:

If you have a number with "zeros" at the end, it may look nice and understandable for a human, but a computer doesn't need them, since the number is still mathematically correct when omitting the zeros.
e.g. "123.00" is the same as 123 or 123.000000
It is only a question of formatting the output when printing or displaying the number again (in which case the number will be casted back into a string). You can do it like this:

    String numericString = "2456.00 ";  // your double as a string
    double doubleValue = Double.parseDouble(numericString);  // parse the number as a real double
      // Do stuff with the double value
    String printDouble = new DecimalFormat("#.00").format(doubleValue); // force the double to have at least 2 digits after the .
    System.out.println(printDouble);  // will print "2456.00"

You can find an overview on DecimalFormat here.

For example the # means "this is a digit, but leading zeros are omitted" and 0 means "this is a digit and will not be omitted, even if zero"

hope this helps

GameDroids
  • 5,584
  • 6
  • 40
  • 59
0

The number you want to parse into Double contains "," and space so you need first to get rid of them before you do the parsing

String str = "1234, 2456.00".replace(",", "").replace(" ", "");
double Val=Double.parseDouble(str);
Alan M
  • 616
  • 5
  • 15
  • Hi..Sorry for the unclear question. There were some typo errors. I was trying to parse the string value 2456.00 to double. But the result I got was 2456.0 What can be done so that I get the value as 2456.00 itself? – Aish Sep 29 '15 at 04:34
  • you need something called DecimalFormat which extends from NumberFormat please look at []http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java – Alan M Sep 30 '15 at 07:47