for(int i=0;i<100;i++)
{
float val=(i/100);
System.out.println(val);
}
Output:
0.0
0.0
0.0....
Desired Output:
0.0
0.1
0.2
0.3...
I am trying to convert percentage value between 0 to 100 why happened this?
for(int i=0;i<100;i++)
{
float val=(i/100);
System.out.println(val);
}
Output:
0.0
0.0
0.0....
Desired Output:
0.0
0.1
0.2
0.3...
I am trying to convert percentage value between 0 to 100 why happened this?
Since i
& 100
are both integer values, the result of their division will also be an integer. Hence, you need to convert at least one of them (the division operands) to a float
, & divide by 10 instead:
for(int i=0;i<10;i++) // Change the loop step to 10 also
{
float val = (float)i / 10; // or float val = i / (float)10;
System.out.println(val);
}
Output:
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
i
and 100
are both integers, so will return an integer. You're dividing integers, which means that you're using integer division.
In integer division the fractional part of the result is thrown away.
Try doing this:
float val = (i / 100f);
Your problem is that the compiler is performing integer division on 2 integers, then casting the result (0) to a float. To avoid this, add an explicit cast to either side of your division. This will cast earlier and perform floating point division.
float val = (float) i / 100;
// output
0.0
0.01
0.02
0.03
...
Try to make the change as:
float val=(i/100f);
As of now you are dividing an int by an int which returns and int and hence you are not getting the desired result.
You need to divide float
to get float
, otherwise int / int
just give an int
and you are assigning it to a float
.
float val=(Float.valueOf(i)/100);
The reason is java promotion rules. in the expression , (i/100) both are integer so result is integer,. 99/100 is o.99 , when converted to int it becomes 0.
According Java Language Specification (Numberic Promotion 5.6
Numeric promotion (§5.6) brings the operands of a numeric operator to a common type so that an operation can be performed.
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
1. If any operand is of a reference type, it is subjected to unboxing conversion
(§5.1.8).
2. Widening primitive conversion (§5.1.2) is applied to convert either or both
operands as specified by the following rules:
• If either operand is of type double, the other is converted to double.
• Otherwise, if either operand is of type float, the other is converted to float.
• Otherwise, if either operand is of type long, the other is converted to long.
• Otherwise, both operands are converted to type int.
The data types of i
and 100
are integer in your expression. Integer Division (int/int)
is computed as Math.floor(int/int)
(equivalent operation) and it always returns zero in your case.
try using one of the following expresssions to convert atleast one of the operands to float or double.
float val=(float)i/100
or
float val=i/(float)100
or
float val=i/100.0f
It's because you're doing integer division.
Divide by a double or a float, and it will work:
double scale = ( n / 1024.0 ) * 255 ;
Or, if you want it as a float,
float scale = ( n / 1024.0f ) * 255 ;
Or
float val=(float)i/100; // covert i to float and divide