0

How to convert string value into integer and multiply the converted value to integer so that I can get the result as 300.00. See example below -

int value = 5;
String str = "60.00 Cur";

If I code like these then I am getting error -

Float f = new Float(app.price.get(position)); 
double d = f.doubleValue(); 
int i = (int)d;
asked Oct 7, 2013 at 10:36
2
  • 2
    Can you please clarify the question? Commented Oct 7, 2013 at 10:39
  • Please refer stackoverflow.com/questions/how-to-ask Commented Oct 7, 2013 at 10:42

7 Answers 7

2

Use Interger.parseInt(String) or Float.parseFloat(String)

refer API documentation

http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html

answered Oct 7, 2013 at 10:39
2

Use Integer.parseInt() method to convert to integer.

answered Oct 7, 2013 at 10:40
1

try this

int n = NumberFormat.getIntegerInstance().parse(str).intValue();
answered Oct 7, 2013 at 10:39
0

Can you try the following code and see if this is what you want? The final answer will be present in the variable "result".

String str = "60.00 USD";
int value = 5;
String dollarValue = str.split(" ")[0];
Float price = Float.parseFloat(dollarValue );
Float result = price * value;
DecimalFormat df = new DecimalFormat("#.##");
df.setMinimumFractionDigits(2);
String resultString = df.format(result);

You will get a "NumberFormatException" if you try to parseFloat on str directly as it contains the string USD. Here we will split that and take the first part.

Edit: Try the newly added code with DecimalFormat and use the resultString for your purpose.

answered Oct 7, 2013 at 10:42
2
  • i want to get result as 300.00 it is getting as 300.0 Commented Oct 7, 2013 at 11:06
  • I have updated the answer with some formatting using DecimalFormat. Can you try with this code? Commented Oct 7, 2013 at 11:23
0

If the String only contains numeric values you can use

Integer.parseInt(string); or Float.parseFloat(string);

As for your example, the "USD" part will cause this to fail, so you'll probbaly have to strip this part out first.

e.g. string.split(" ");

answered Oct 7, 2013 at 10:41
0

you will get a NumberFormatException as "60.00 USD" is not a float. To make it right you have to remove the currency. There can be many ways to remove the usd. For example

String s = "60.00 USD";
s = s.replace(" USD" , "");

or if the currency is not fixed

s = s.split(" ")[0];

Now you have A string s whose value is "60.00"

so you can do this now

float f = Float.parseFloat(s);
answered Oct 7, 2013 at 10:47
0

Use,

 Integer.parseInt("value you want to parse as int")

likewise you can use,

Double.parseDouble("value");

This is answer for your Question

int result = Integer.parseInt("150.00 USD")* Integer.parseInt("5");
answered Oct 7, 2013 at 10:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.