刚刚吃好饭,写了一个! public class RoundTest { /*a表示原来的数据,b表示截取的位数*/ public static double roundChange(double a ,int b) { if(b < 0) return a; int k = 1; for(int i = 0; i < b; i++) { k = k * 10; } return ((double)Math.round(a*k))/k; } /*test the method*/ public static void main(String[] args) { double a = 8.454; double b = 3.64; double c = a/b; System.out.println(RoundTest.roundChange(3.456,2)); } }
import java.text.DecimalFormat;public class ffff { public static void main(String[] args) { NumberFormat doubleFormat = new DecimalFormat(".00"); double pprice = 0.0; pprice = (double) (29.7 / 17.5); System.out.print(doubleFormat.format(pprice)); } }
to :ECNU_SEI_kingsang(myjava) 和 cxz7531(大花猫) 谢谢两位,两位的程序都写的很不错,完全达到了要求,谢谢参与!
public class RoundTest {
/*a表示原来的数据,b表示截取的位数*/
public static double roundChange(double a ,int b) {
if(b < 0) return a;
int k = 1;
for(int i = 0; i < b; i++) {
k = k * 10;
}
return ((double)Math.round(a*k))/k;
}
/*test the method*/
public static void main(String[] args) {
double a = 8.454;
double b = 3.64;
double c = a/b;
System.out.println(RoundTest.roundChange(3.456,2));
}
}
NumberFormat doubleFormat = new DecimalFormat(".00");
double pprice = 0.0;
pprice = (double) (29.7 / 17.5);
System.out.print(doubleFormat.format(pprice));
}
}
谢谢两位,两位的程序都写的很不错,完全达到了要求,谢谢参与!