This question already has answers here:
Floating point arithmetic not producing exact results [duplicate]
(7 answers)
double displaying very long number
(4 answers)
Closed 8 years ago.
Why do I get 0.6100000000000001 when I should get 0.61 ? I know the get I know the decimal format to fix it. I just want the explanation for why it happens.
public class Mid2009{
public static void main (String args []){
double d1 = 1.03;
double d2 = 0.42;
double d3 = d1-d2;
System.out.println(d3);
}
}
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Java explicit type cast for the floating-point division
(2 answers)
Closed 2 years ago.
static void CToF(float c){
float f=32+((9/5)*c);
System.out.printf("%.2f",f);
}
public static void main(String[] args) {
CToF(27);
sc.close();
}
Here the priority of the * and / same so according to their associativity , it will execute from right to left.
So that 9/5 executed first and after that it multiplies with c so the answer is 80.60 but I got 59.00.
What's the problem?
if I make some change and write like this
float f=32+(c*9/5);
it works.xD
This question already has answers here:
Why converting from float to double changes the value?
(9 answers)
Closed 2 years ago.
This is the code
public static void main(String[] args) {
double x=5.6556464566546546546556465465465;
float y=(float)x;
double z= 1+y;
System.out.println(x+"\n"+y+"\n"+z);
}
}
and this is the output
5.6556464566546545
5.6556463
6.655646324157715
I can understand the value of x and y but z from where it got those fractional numbers after the 3??!
Thank you very much
Floats are an approximation of the actual number in Java, due to the way they're stored. If you need exact values, use a BigDecimal instead.
This question already has answers here:
round up to 2 decimal places in java? [duplicate]
(12 answers)
Closed 5 years ago.
I'm doing a calculation on android studio (java) and the answer that I get back is like 4.654783632444251. I don't want the long answers. Preferably Two Decimal places would be ideal. Using Double.parseDouble
You can round it to two decimal places:
Math.round(myNumber * 100) / 100
Or you can format it using
String.format("%.2f", myNumber)
The second method even prints the decimal places if they are 0.
protected double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
Just call this method entering your value and 2 places.
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I am using Java 7
I am trying to get a double from dividing two integer:
double chance = 1/main.getConfig().getInt("dailygiftItems.items."+key+".chance");
double w = Math.random();
System.out.println("Pulled: "+main.getConfig().getInt("dailygiftItems.items."+key+".chance"));
System.out.println("itemChance: "+chance);
System.out.println("itemRand: "+w);
if(!(w < chance))continue;
However chance is returning 0. As you can see I have debugged all the values here is what they bring me:
Pulled: 5
itemChance: 0
itemRand: some random double (working correctly)
I was thinking if I did my math wrong and 5/1 is not 0.2 so I used a calculator. However the calculator returned to me 0.2.
I then tested on something simpler testing the same problem:
public class Test
{
public static void main(String[] args)
{
double chance = 1/5;
double w = Math.random();
System.out.println("Chance: "+chance);
System.out.println("Random: "+w);
if(!(w < chance))
{
System.out.println("no");
return;
}
System.out.println("yes");
}
}
This produced the same result as:
Chance: 0
w: some random double (working)
My questions: Why is java not dividing this correctly, and how can I fix it?
Use double chance = 1./main.getConfig().getInt("dailygiftItems.items."+key+".chance");
The . after 1 will force the compiler into considering this is a floating point (double) operation.
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I wanna do this math in java:
int index = 3 * (9568/20001);
in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..
but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.
The first postion int 1 of 1.43512824 is what I want to get.
Try this
int index = (int)3 * (9568.0/20001);
Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.