Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Type conversion

  Asked By: Darla    Date: Feb 28    Category: Java    Views: 1336

I am passing a variable from one jsp page to other.

I am receiving it using request.getParameter("value");

But this is a value that is float value. I need to store it in a
float variable.

I defined a float variable
float v_value;

And then
v_value = request.getParameter("value");

It did not work. It gave me an error. I tried this way too:

String s_value;
float v_value;
s_value = request.getParameter("value");
v_value = s_value;

But did not work.

Please advise me.



5 Answers Found

Answer #1    Answered By: Mansur Bashara     Answered On: Feb 28

You can't directly store  string in float. so you have to parse string  or convert
it into float.
see this method
parseFloat(String s)
this is of string class and return float  representation of the string you're

Answer #2    Answered By: Farah Khan     Answered On: Feb 28

From JBuilder help

try {
f = Float.parseFloat(gg.trim());
catch (NumberFormatException e) {

where gg is your string  and f is your float.

Answer #3    Answered By: Eline Bakker     Answered On: Feb 28

you use static method 'parseFloat(String s)' from float  class to convert
string to float

float f = Float.parseFloat(stringFloat);

every basic type  has it's coresponding class in java that you can use to do
operations regarding to the type
int -> java.lang.Integer
float -> java.lang.Float
boolean -> java.lang.Boolean
.. etc

Answer #4    Answered By: Harriet Hughes     Answered On: Feb 28

The return type  of request.getParameter("value") is string. Do typecasting to
convert into double value.

Answer #5    Answered By: Blandina Garcia     Answered On: Feb 28

You have to explicitly convert the string  to a float. There are more than one
to do this but a good one is:

v_value = new Float(s_value).getValue(); // (instead of v_value = s_value;).

Didn't find what you were looking for? Find more on Type conversion Or get search suggestion and latest updates.