Link to home
Start Free TrialLog in
Avatar of keviin555
keviin555

asked on

12 May 11 16 - compare float

hello,

i have
String m = req.getParameter("montant");
float mont = Float.parseFloat(m);  

and this
session.setAttribute("solde",new Float(rs.getString("solde")));
f = ( (Float) session.getAttribute("solde") ).floatValue();


i want to make :

if (f>m)
{
f-=m;
}

but it dont work
ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
float - is primitive reprsenting float number

Float - is object - wrapper around the float primitive
Float is not the same as float, you cannot add two Float objects,
you cannot multiply them, etc
float number is just a field of the Float object.
therefore sya floatValue() is a method of Object Float which exposes
this primitive field; new Float(float f) creates object with the primitive field eqqual to f.
Float.parseFloat(String s)  - static method of the Float class - kind of convenience
method which goes into details nanlyses all characters of string and figures out
if these characters make up something that may reoresent float primitive number
and returns this number, or, alternaticvely, throws exception indicating that this String is no
good for making up a float number
Static method has nothing to do with any particular Float isnstance

method req.getParameter("name")
always returns String, that String may either represent a number, or a text or anything,
but at the moment when you get it
m = req.getParameter("name"), m is just a String

Therefore if(f>m)  makes no sense, f is float, m is String
What you can do is to analyze what kind of Struing is m (see my previous post)
and maybe ity is inded can be converted to number
then you can make

float f1 = Float.parseFlaot(m);
and then you can compare
  f and f1
if(f>f1) will be valide syntax

 


Avatar of keviin555
keviin555

ASKER

ohhh i'm so stupid :/
i thout that Float and float are dirrerent
Float and float are different!!!
Float - object
float - primitive number
Avatar of Mick Barry
What was wrong with what cmalakar posted?
Looks like exactly what was needed.


> float f1 = Float.parseFlaot(m);
> and then you can compare
>  f and f1
> if(f>f1) will be valide syntax

thats exactly what cmalakar had already suggested
>>What was wrong with what cmalakar posted?

I agree ;-)

And the comment from author "ohhh i'm so stupid :/" was at same time, as accepted solution's comment.
I wonder how it was selected.
Yes, my first comment was completely ignored, which has the solution for the question, and it suggests the same as the accepted solutions comment.

"objects" thanks for asking the explanation.