Not getting the right answer from celcius to farenheit [duplicate] - java

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

Related

Why casting give me these weird results Java? [duplicate]

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.

cast (int) does not work correctly [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 7 years ago.
I initialized a double a with Math.pow(10,24).
Now I need to convert double a to int b:
a = Math.pow(10,24)
int b = (int)a;
System.out.println(a);
System.out.println(b);
System prints out :
1.0E24
2147483647
This result is obviously not correct.
It's because Integer has limit INT_MAX - 2147483647

Java dividing incorrectly [duplicate]

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.

How to get a int from a long double in java? [duplicate]

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.

Calculate the difference between 2 double numbers [duplicate]

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);
}
}

Categories