Calculation always results in zero [duplicate] - java

This question already has answers here:
Division in Java always results in zero (0)? [duplicate]
(3 answers)
Is "long x = 1/2" equal to 1 or 0, and why? [duplicate]
(6 answers)
Closed 8 years ago.
I have the following java code
long x=25782
float y=x/1000000000
Instead of y being equal to 0.000025782 as I would expect, it is equals 0.0
What am I doing wrong?

You're dividing by an long type, so it will round down to 0.
Use this instead:
float y=x/1000000000.0;

Integer division gives an integer result
This is integer division; in integer division the answer is always an integer (by truncating the non integer part, not be rounding). Which you are then casting to a float.
The easiest solution is to include at least 1 float. So
long x=25782
float y=x/1000000000f //(or 1000000000.0)
What you are effectively doing
float y=x/1000000000;
float y=(float)(int)(x/1000000000);
float y=(float)(int)(25782/1000000000);
float y=(float)0;
float y=0.0;

You have an integer division, which will round to the right of the decimal point, as integer division gives integer result.
You should cast x to float.
Try:
float y=(float)x/1000000000;

Int and long division always result in int and long.
at least operand in your calculation needs to be a float or double

Related

Java data types [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
int totalOptCount = 500;
int totalRespCount=1500;
float percentage =(float)(totalOptCount/totalRespCount);
Why does this always return value 0.0? Also I want to format this into 00.00 format and convert into string?
Because the conversion to float happens after the division has been done. You need:
float percentage = ((float) totalOptCount) / totalRespCount;
You should be able to format using something like:
String str = String.format("%2.02f", percentage);
If you are using int values, using a double may be a better choice and have less rounding error. float can represent int values without error up to ~16 million. double can accurately represent all int values.
double percentage =(double) totalOptCount / totalRespCount;
Percentages are usually multiplied by 100, meaning you can drop the cast.
double percentage = 100.0 * totalOptCount / totalRespCount;
(totalOptCount/totalRespCount)
here both dividend and divisor are of type int which means they will allow only integer values and the answer of such equation will always be an integer literal.
if I break this it will be something like below
(double)(500/1500)
According to the actual calculation, 500/1500 will give you 0.33333 but compiler will convert this into integer literal because both operands are of type int
(double)(0)
Compiler gets an instruction to cast this 0 value to double so you got 0.0 as result
0.0
and then you can change the result to any format as suggeted by #Zach Janicki.
keep in mind if both the operands are of same type than result will be of same type too.
Integer division (which includes long, short, byte, char, int) in Java always returns an int (or long, if one of the parameters is long), rounding towards zero. Your conversion occurs after this calculation.
(The formatting question is already answered by the other answers - alternatively you could also have a look at java.text.NumberFormat, specially java.text.DecimalFormat.)
String.format("%2.02f", (float)totalOptCount/totalRespCount);
to format a double and print out as a percentage, you can use use
System.out.println(new DecimalFormat("##.##").format(yourDouble) + "%"));

Android Studio not calculating float value...or am I dumb? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
int totalOptCount = 500;
int totalRespCount=1500;
float percentage =(float)(totalOptCount/totalRespCount);
Why does this always return value 0.0? Also I want to format this into 00.00 format and convert into string?
Because the conversion to float happens after the division has been done. You need:
float percentage = ((float) totalOptCount) / totalRespCount;
You should be able to format using something like:
String str = String.format("%2.02f", percentage);
If you are using int values, using a double may be a better choice and have less rounding error. float can represent int values without error up to ~16 million. double can accurately represent all int values.
double percentage =(double) totalOptCount / totalRespCount;
Percentages are usually multiplied by 100, meaning you can drop the cast.
double percentage = 100.0 * totalOptCount / totalRespCount;
(totalOptCount/totalRespCount)
here both dividend and divisor are of type int which means they will allow only integer values and the answer of such equation will always be an integer literal.
if I break this it will be something like below
(double)(500/1500)
According to the actual calculation, 500/1500 will give you 0.33333 but compiler will convert this into integer literal because both operands are of type int
(double)(0)
Compiler gets an instruction to cast this 0 value to double so you got 0.0 as result
0.0
and then you can change the result to any format as suggeted by #Zach Janicki.
keep in mind if both the operands are of same type than result will be of same type too.
Integer division (which includes long, short, byte, char, int) in Java always returns an int (or long, if one of the parameters is long), rounding towards zero. Your conversion occurs after this calculation.
(The formatting question is already answered by the other answers - alternatively you could also have a look at java.text.NumberFormat, specially java.text.DecimalFormat.)
String.format("%2.02f", (float)totalOptCount/totalRespCount);
to format a double and print out as a percentage, you can use use
System.out.println(new DecimalFormat("##.##").format(yourDouble) + "%"));

Why does "double z = 1/3" result in 0.0? [duplicate]

This question already has answers here:
Unexpected result in long/int division
(7 answers)
Closed 8 years ago.
I am trying to learn interesting behaviour of java. Please explain
double z = 1/3;
System.out.println(z);
This program return 0.0 where as
double z = 1/3d;
System.out.println(z);
This program prints 0.333333. What is the difference.
The first one is an integer division really. It divides an integer by another integer (the result of which is again an integer) and assigns the result to a double variable.
Only the second one yields a double as result.
integer / integer => result is integer, even though assigned to a double variable
integer or double / double => result is double
first example will divide integers and assign them to your double "z", second divides doubles in the first place
Integer/Integer is an Integer even if you assign it to a holder variable of type double.To get a double precision value,you need to do your arithmetic operations using double variables

Floating point in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does (360 / 24) / 60 = 0 … in Java
Having problems with floats and longs in Java
float f = 0.100f;
f = 3/180;
At the minute i am trying to do something like this with object and their attributes, but even to this simplest form my program returns 0.0.
I have tried this with Longs as well as still the same result. It's been a long day and maybe it's something simple but I'm at a brick wall.
Your expression 3/180 is performing an integer division, and it is then casting that into the float f. In integer division, 3/180 will return 0, and this is what you are seeing.
What you probably want to do is just add a decimal point to your numbers: f = 3.0/180.0;
3/180 is integer division.
Therefore, the result is truncated to an integer.
You need to perform floating-point division: 3/180f
You do an integer division. So you get an int which is casted back to a float. Try this:
f = 3/180.0;
or
f = 3/180f;
Try 3.0/180. Otherwise, you are dividing two integers and you run into integer truncation. When you do integer division the result is also an integer, not a floating point number.

What's wrong with this division? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
Here's the code:
class testsum
{
public static void main(String arg[])
{
double sum=0;
double fraction;
fraction=-1/9;
System.out.println("fraction: "+fraction);
fraction=-1;
fraction=fraction/9;
System.out.println("fraction: "+fraction);
}
}
the outputs are 0 and then -0.11111111
why was the first output 0 and not -0.11111111111?
It's doing integer division in the first example as this is the default type for a numeric literal. Try changing it to -1.0/9 (or 1d/9d - the d suffix indicates a double) and you should get the same answer.
1 and 9 are both integer. Try
1.0/9
This is why it works for fraction/9, since fraction is a double.
When you do -1/9, it says "-1, that's an int. 9, that's an int. -1 / 9 in integer division is 0. Oh, now I need to cast to double."
Changing it to -1.0 / 9 should solve the problem.
Try wrapping the "-1/9" in brackets.
The first one is 0 because it is doing integer division. -1 and 9 and integers and when divided equal 0. The result is then converted into a double so it can be stored in fraction. The easiest solution is this:
fraction = -1.0/9;
Because -1 and 9 are integers, so -1/9 is an integer division (with the result 0, which when cast to double is 0.0).
To do a floating point division, you should convert one of the numbers to double, (double) 9, 9d or simply 9.0.
In the latter case, fraction is already double (-1.0) so fraction/9 is a floating point division.

Categories