+ 3
Please am having a lot of difficulties in using float and double is there anyone that can please correct this code
class Formulas{ static float CSA(int x,float pi,int r,int h){ return x*pi*r*h; } public static void main(String[]args){ float csa=CSA(2,3.142,4,5); System.out.println(csa); } }
4 Answers
+ 14
class Formulas{
static float CSA(int x,float pi,int r,int h){
return x*pi*r*h;
}
public static void main(String[]args){
float csa=CSA(2,3.142f,4,5);
/*U can convert int to double , float to double but can't convert double to int or double to float directly*/
System.out.println(csa);
}
}
+ 5
public class Program
{
static float CSA(int x,double pi,int r,int h){
return x*(float)pi*r*h;}
public static void main(String[]args){
float a = CSA(2,3.142,4,5);
System.out.println(a);
}
}
+ 5
I cant be certain that my method is the best way to achieve this as precision could be lost due to casting from double to float maby @Gaurav could help with this one đ
+ 3
but please I want to ask why is it that the first pi is in double while the others are float can't I just make everything float or is there a rule behind it