Java Methods determine how many times to output char based off of paramaters -


i've done searching , cannot determine how looking do.

i have method rounding numbers takes paramaters numbertoround , decimalplaces right hardcoded round 4 decimal places need dynamic. if called round(3.1415926536, 3) round 3 decimal places rather 4 cannot figure out how java take 3 , understand want 000 printed or handled anyways.

any ideas on how this? below sample code have typed hard coded 4 places since nothing trying compile right

public class round {   public static void main(string[] args)    {       system.out.println("pi rounded 3 places " + roundpi(3.1415926536, 3));   }   public static double roundpi(double numbertoround, int decimalplaces)   {      numbertoround= (double)math.round(numbertoround*10000)/10000;     return numbertoround;   } } 

current output

pi rounded 3 places 3.1416

i considered doing math function of kind 10*100 1000 round 3 if need 4 or 5 decimal places or 2 still need figure out how figure out needs multiply without hardcoding if statement mile long handle if change im rounding intrest calculator , user decides want rounded 10 or 12 or more or less 1 decimal place.

poweroften    = math.pow(10,decimalplaces); numbertoround = (double)math.round(numbertoround * poweroften) / poweroften; 

that round correct number of places. when print, need ensure right number of trailing zeroes printed. that, can use system.out.printf.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -