5
float NormValue = value*80 ;
float color = Color.argb(0xFF, NormValue, 0, 0);

This is a part of my code. This variable (NormValue) stores the result in float . But in second line i cannot use this variable since it has to be converted to int. How can i do it. Any help?

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Aswathy
  • 337
  • 2
  • 8
  • 18

7 Answers7

13

Try this..

No need to typecast float to int just use Math.round()

float NormValue = value*80 ;
float color = Color.argb(0xFF, Math.round(NormValue), 0, 0);
Hariharan
  • 24,741
  • 6
  • 50
  • 54
6

Depends on the scale of your NormValue.

Normally, a simple type cast would do:

(int)NormValue

But you may want to scale the NormValue to the range 0..255 first since Color.argb() just uses the least significant 8 bits of the int values passed in.

laalto
  • 150,114
  • 66
  • 286
  • 303
3

Try this.

Convert it to string

String.valueOf(value);

and then convert it to integer

Integer.valueOf(string);

You can also try casting it to int

Int i =  (int) your_float_value
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
1

Try this:

float NormValue = value*80 ;
int temp = (int)NormValue;
float color = Color.argb(0xFF, temp, 0, 0);
KethanKumar
  • 716
  • 5
  • 17
1

Android is java, right? You can simply cast it to int like this.

float f=0.2;
int i;

i = (int)f;
brunch875
  • 859
  • 5
  • 15
0

You really need to use float in the first place? You have two options:
1. Use casting int normInt = (int)NormValue; but here you have to be prepared that you will loose the decimal points. eg: 8.61 will be 8
2. The second way to first round the NormValue then use casting.

Alex
  • 3,382
  • 2
  • 32
  • 41
-3

You can search in this post, your solution is there: Java: (int)(float)Float.valueOf(s) or Float.valueOf(s).toInt()

If you want more information, you can ask me :)

Community
  • 1
  • 1
Kappys
  • 673
  • 2
  • 7
  • 20