Solved

compare strings

Posted on 2006-04-12
218 Views
String a ="53.235"
String b ="95"

how to compare them and get b is greater than a. 95>53.235
there is Interger.getInt("53.235"), but 53.235 is not int.

0
Question by:keepworking

LVL 5

Expert Comment

Float.getFloat("95").compareTo(Float.getFloat("53.235"))
0

LVL 5

Assisted Solution

Sorry

Float.valueOf("95").compareTo(Float.valueOf("53.235"))
0

LVL 14

Expert Comment

Double.parseDouble(a).compareTo(Double.parseDouble(b));
0

LVL 5

Accepted Solution

Returns:
the value 0 if anotherFloat is numerically equal to this Float; a value less than 0 if this Float is numerically less than anotherFloat; and a value greater than 0 if this Float is numerically greater than anotherFloat.

0

LVL 5

Assisted Solution

use Float.parseFloat or Double.parseDouble

0

LVL 11

Assisted Solution

you can use
float aa = Float.parseFloat(a);
float bb = Float.parseFloat(b);
and after that compare the float values.
0

LVL 5

Assisted Solution

>> hoomanv
Double.parseDouble returns a double and not a Double object.
valueOf should be used here.

or

Float.compare(Float.parseFloat(a), Float.parseFloat(b));
Double.compare(Double.parseDouble(a), Double.parseDouble(b));
0

LVL 14

Expert Comment

mrigank
you are rigth ;-)
0

LVL 11

Expert Comment

If we have
float aa = Float.parseFloat(a);
float bb = Float.parseFloat(b);

the comparison could be done simply by <, > and =
0

LVL 5

Expert Comment

:-) no worries.
0

Featured Post

Suggested Solutions

hash value 2 29
Receive file in Servlet 1 28
Updating Java 9 62
stringclean challenge 26 43
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…