 This topic has 6 replies on 1 page. 


Posts:341 Registered: 030612  Math.round Aug 9, 2004 7:21 AM 

 I found the function I want to use. But I am not sure how to use it. I have a class called FtoC and i return converstion of Fahrenheit to Celsius class FtoC{public static double getTemp(double tempF){ return (5.0 * (tempF  32.0) / 9.0);}}//end class FtoC
now i want to round the converstion so i use the Math.round function in my return class FtoC{public static double getTemp(double tempF){ return Math.round(5.0 * (tempF  32.0) / 9.0);//used Math.round}}//end class FtoC
so if i input my tempF as 40 F i receive my answer as 4.0 converted to C but i actually want a two decimal place display so my return looks like 4.45 how do i set the Math.round fucntion to adjust for decimals places??? any suggestions??? thanks in advance annie :)  
Posts:67 Registered: 3/13/02  Re: Math.round Aug 9, 2004 7:29 AM (reply 1 of 6) 

 Multiple input by 100, call Math.round(), and then divide the result by 100?  
Posts:341 Registered: 030612  Re: Math.round Aug 9, 2004 7:30 AM (reply 2 of 6) 

 public static double getTemp(double tempF){ R = Math.floor(R * 100.0 + 0.5) / 100.0; R = Math.round(100.0 * R) / 100.0; return Math.round(5.0 * (tempF  32.0) / 9.0);}
im playing around with this now but its giving me an error about my class. I guess the R is hurting me!!!  
Posts:341 Registered: 030612  Re: Math.round Aug 9, 2004 7:32 AM (reply 3 of 6) 

 first the R should be tempF because that is what i call it, but im still getting a weird result???  
Posts:24,036 Registered: 2/3/03  Re: Math.round Aug 9, 2004 7:34 AM (reply 4 of 6) 

 To format decimal numbers, look at the java.text.DecimalFormat class.  
Posts:67 Registered: 3/13/02  Re: Math.round Aug 9, 2004 7:37 AM (reply 5 of 6) 

 Something like this should work, though I haven't tried it ... tempF *= 100.0;tempF = Math.round(tempF);tempF /= 100.0; return tempF;
 
Posts:341 Registered: 030612  Re: Math.round Aug 9, 2004 7:40 AM (reply 6 of 6) 

 tempF = (5.0 * (tempF  32.0) / 9.0); tempF *= 100.0; tempF = Math.round(tempF); tempF /= 100.0; return tempF;
this works!!!! cheers!  