java编写一个将实数四舍五入到小数点后第n位的函数,并调用此函数将一个实数舍入到
public static void main(String[] args) {
ss(435.546546546,5);
}
public static void ss(double f,int n){
System.out.println(String.format("%."+n+"f", f));
}
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static double round(double d, int n)
{
return ((int)(d * Math.pow(10.0, (double)(n)) + 0.5)) / Math.pow(10.0, (double)(n));
}
public static void main (String[] args) throws java.lang.Exception
{
double d = 12.34567890123;
d = round(d, 1);
System.out.println(d);
d = 12.34567890123;
d = round(d, 2);
System.out.println(d);
d = 12.34567890123;
d = round(d, 3);
System.out.println(d);
d = 12.34567890123;
d = round(d, 4);
System.out.println(d);
d = 12.34567890123;
d = round(d, 5);
System.out.println(d);
}
}
12.3
12.35
12.346
12.3457
12.34568