-1

I need to get a float value based on percentage.

For example:

1%: 0.01 10%: 0.10 25%: 0.25 50%: 0.5 And so on.

I've tried getting a 0-100 percent value(Which works) and uploading it to a string:

String value = "0." + percent;

and parsing it as a float:

Float.parseFloat(value);

But that doesn't work. Any help is appreciated, thanks!

5
  • Your question is not clear. Are you asking how to display a number between 0 and 1 in binary?
    – Eran
    Oct 23, 2014 at 12:39
  • if (s == "0.100") - Compare Strings with .equals.
    – Maroun
    Oct 23, 2014 at 12:40
  • 5 out of 10, therfore 50% should be 0.1?
    – Patrick
    Oct 23, 2014 at 12:42
  • are you looking for (x - min) / (max - min)?
    – Blackbelt
    Oct 23, 2014 at 12:44
  • Absolutely no idea what you want to achieve. What do you mean with "0-1 percentage"? And why is 5 out of 10 0.1?!
    – Uwe Allner
    Oct 23, 2014 at 12:44

2 Answers 2

2

float percentage = (float)intPersentValue/100.0f; this will give floating value like 25 to 0.25

0

I did not actually get the question but are you trying to get

0.1 => 1.0 
0.015 => 0.1

or

0.1  => 100% 
0.01 => 10%

If you want 5/10 => 0.5 you can use:

int a = 5;
int b = 10;
float percentage = (float)a/float(b);

If you can elaborate a bit more I can help.

6
  • Well the idea is to set the progress of a View that requires a float 0-1 to the percentage of a cap and number. Basically getting a percentage of 0 to 100 and turning it to a float 0-1 Oct 23, 2014 at 13:03
  • so you need to use @blackbelt 's answer. (float) i/ (float)iCap If we set min = 0.0.
    – Hakes
    Oct 23, 2014 at 13:07
  • We removed the min because min is 0.0 already in (x - min) / (max - min). We just added a typecast to float. Without typecase: 10/100 = 0
    – Hakes
    Oct 23, 2014 at 13:14
  • so x/max? Maximum is one so it would stay the same. Oct 23, 2014 at 13:50
  • Maximum is 100 in you case.
    – Hakes
    Oct 23, 2014 at 15:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.