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
keviin555Asked:
Who is Participating?
 
cmalakarConnect With a Mentor Commented:
it should be

if(f > mont){
f -= mont
}

Open in new window

0
 
for_yanCommented:
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

 


0
 
keviin555Author Commented:
ohhh i'm so stupid :/
i thout that Float and float are dirrerent
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
for_yanCommented:
Float and float are different!!!
Float - object
float - primitive number
0
 
objectsCommented:
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
0
 
cmalakarCommented:
>>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.
0
 
cmalakarCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.