1

I am getting an integer value in my android application.I want to convert it into floating point number which is in this format

"0.xyF"

.I tried lot of methods.I know its simple but i am confused.Please help.

I am passing a value from one activity to another using putExtra.So in the second activity i have to convert it to float for setting the value as verticalMargin for my dialog window.I used this line for getting the value in second activity.

int data = getIntent().getIntExtra("value", 7);

This is used for setting the vertical margin.

wlp.verticalMargin = "the converted floating point number";
Nevaeh
  • 1,519
  • 7
  • 24
  • 45
  • what did you try? you want a float or a string representation? –  Mar 21 '14 at 06:36
  • related: http://stackoverflow.com/questions/4377842/how-can-i-convert-integer-into-float-in-java?rq=1 –  Mar 21 '14 at 06:37
  • Provide sample input/output. [`Float.parseFloat("0." + theInt)`](http://developer.android.com/reference/java/lang/Float.html#parseFloat(java.lang.String)) *might* do what you expect.. if theInt represents `xy` *and* cannot have leading zeros. – user2864740 Mar 21 '14 at 06:37

4 Answers4

0

If i is the integer value, then try:

float f=i;
while(f>=1.0f)
    f/=10.0f;
skrtbhtngr
  • 2,223
  • 23
  • 29
0

You question still isn't clear. If you're asking how to convert an integer value that represents a percentage from 0 to 100 into a floating point value, then it would be fpVal = intVal / 100.0;

scottt
  • 8,301
  • 1
  • 31
  • 41
  • I got an integer value 48 as result in my first activity which i want to pass to next activity.This value has to be converted into floating point number so that i can use it for setting the vertical margin for my dialog window.Am i clear now ? – Nevaeh Mar 21 '14 at 06:55
  • NO, I'm still not sure. I added a separate answer that assumes a completely different meaning to your question. If that still isn't what you're after, perhaps you could provide a couple of before and after values in your question so we can understand your conversion needs. – scottt Mar 21 '14 at 07:10
0

In your first activity store your integer value in string like this

String margin = "0."+int_value;

then pass this string to second activity.

In the second activity get that string from extra and convert it to float.

float float_value = Float.parseFloat(margin);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
umerk44
  • 2,797
  • 4
  • 23
  • 43
0

If you just want a simple conversion of an integer into a floating point number with the same exact value (e.g. 7 --> 7.0), then you can just cast it: fpVal = (float) intVal;

scottt
  • 8,301
  • 1
  • 31
  • 41