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

10th Feb 2018, 9:25 AM
Fakorede Damilola
4 Antworten
+ 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); } }
10th Feb 2018, 12:47 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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); } }
10th Feb 2018, 10:26 AM
D_Stark
D_Stark - avatar
+ 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 😜
10th Feb 2018, 12:45 PM
D_Stark
D_Stark - avatar
+ 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
10th Feb 2018, 10:36 AM
Fakorede Damilola