• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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
0
keviin555
Asked:
keviin555
1 Solution
 
cmalakarCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now