public class MyRound { public static double myRound (double x, int y) { long divided = 1; for(int i = 0; i < y; i++){ x = x * 10; divided = divided * 10; } return (double)((long) (x + .5d)) / divided; }
public static double round(double f, int n) { f *= Math.pow(10, n); f = Math.round(f); f /= Math.pow(10, n); return f; }
改了一点点,控制了输出格式,好像可以 public static String round(double f, int n) { f *= Math.pow(10, n); f = Math.round(f); f /= Math.pow(10, n); DecimalFormat decimalFormat=new DecimalFormat("########0.00"); String returnF=decimalFormat.format(f); return returnF; }
public class MyRound { public static String myRound (double x, int y) { y -= getOffset(x); long divided = 1; for(int i = 0; i < y; i++){ x = x * 10; divided = divided * 10; } return format((double)((long) (x + .5d)) / divided, y); }
public static String round(double f, int n) { n -= getOffset(f); f *= Math.pow(10, n); f = Math.round(f); f /= Math.pow(10, n); return format(f, n); }
public static String doubleToString(double d,int i){ Double f=new Double(d); NumberFormat form; form = (DecimalFormat)NumberFormat.getInstance(); form.setMaximumFractionDigits(i);//设置四舍五入小数点的位数 BigDecimal testBD=new BigDecimal(form.format(f)).setScale(i);//设置最后显示位数 return testBD.toString(); } public static void main(String[] args) {
double testDouble=12.2536; String resultString1=doubleToString(testDouble,3); String resultString2=doubleToString(testDouble,5); System.out.println("3 points' Result of "+testDouble+" is "+resultString1); System.out.println("5 points' Result of "+testDouble+" is "+resultString2); }
chylwk(沧海一浪) 你的是:保留小数点后N位的两种方法。
chylwk(沧海一浪) 你的是:保留小数点后N位的一种方法。
微调: public static String round(double f, int n) { n -= getOffset(f) + 1; f *= Math.pow(10, n); f = Math.round(f); f /= Math.pow(10, n); return format(f, n); }
private static String format(double num, int precision){ String fN = "" + num; boolean hasDot; int index; if((index = getOffset(num)) >= 0) hasDot = true; else hasDot = false; int len = fN.length(); for (int i = precision + 1;i >= len; i--){ if(hasDot) fN = fN + "0"; else fN = "0" + fN; } return fN; }
public static double myRound (double x, int y) {
long divided = 1;
for(int i = 0; i < y; i++){
x = x * 10;
divided = divided * 10;
}
return (double)((long) (x + .5d)) / divided;
}
public static double round(double f, int n) {
f *= Math.pow(10, n);
f = Math.round(f);
f /= Math.pow(10, n);
return f;
}
public static void main(String[] args){
System.out.println(round(18.0/28.0, 5));
System.out.println(myRound(0.6428571428571429,4));
}
}
保留小数点后N位的两种方法。
System.out.println(myRound(0.1,4));
不行啊,输出是
1.0
0.1
public static String round(double f, int n) {
f *= Math.pow(10, n);
f = Math.round(f);
f /= Math.pow(10, n);
DecimalFormat decimalFormat=new DecimalFormat("########0.00");
String returnF=decimalFormat.format(f);
return returnF;
}
public static String myRound (double x, int y) {
y -= getOffset(x);
long divided = 1;
for(int i = 0; i < y; i++){
x = x * 10;
divided = divided * 10;
}
return format((double)((long) (x + .5d)) / divided, y);
}
public static String round(double f, int n) {
n -= getOffset(f);
f *= Math.pow(10, n);
f = Math.round(f);
f /= Math.pow(10, n);
return format(f, n);
}
private static String format(double num, int precision){
String fN = "" + num;
boolean hasDot;
int index;
if((index = getOffset(num)) >= 0) hasDot = true;
else hasDot = false; for (int i = precision;i >= fN.length(); i--){
if(hasDot) fN = fN + "0";
else fN = "0" + fN;
}
return fN;
}
private static int getOffset(double d){
String fN = "" + d;
return fN.indexOf(".");
}
public static void main(String[] args){
System.out.println(round(26,4));
System.out.println(myRound(0.6,4));
System.out.println(round(12.2536, 4));
System.out.println(myRound(1.2,4));
}
}
System.out.println(df.format(1.0265));
select to_char(round('1.0000',2),'9,999.00') from dual
把double转成对象算长度.
不足补0,长了取得你要长充的下一位进行四舍五入.
n -= getOffset(f);
f *= Math.pow(10, n);
f = Math.round(f);
f /= Math.pow(10, n);
return format(f, n);
}
private static String format(double num, int precision){
String fN = "" + num;
boolean hasDot;
int index;
if((index = getOffset(num)) >= 0) hasDot = true;
else hasDot = false;
int len = fN.length();
for (int i = precision;i >= len; i--){
if(hasDot) fN = fN + "0";
else fN = "0" + fN;
}
return fN;
}
private static int getOffset(double d){
String fN = "" + d;
return fN.indexOf(".");
}
public static void main(String[] args){
System.out.println(round(26.1235123,5));
System.out.println(round(0.6,5));
System.out.println(round(12.2536, 5));
System.out.println(round(1.2,5));
System.out.println(round(1.0, 5));
System.out.println(round(0.1,5)); }26.124
0.600
12.254
1.200
1.000
0.100
Double f=new Double(d);
NumberFormat form;
form = (DecimalFormat)NumberFormat.getInstance();
form.setMaximumFractionDigits(i);//设置四舍五入小数点的位数
BigDecimal testBD=new BigDecimal(form.format(f)).setScale(i);//设置最后显示位数
return testBD.toString();
}
public static void main(String[] args)
{
double testDouble=12.2536;
String resultString1=doubleToString(testDouble,3);
String resultString2=doubleToString(testDouble,5);
System.out.println("3 points' Result of "+testDouble+" is "+resultString1);
System.out.println("5 points' Result of "+testDouble+" is "+resultString2);
}
你的是:保留小数点后N位的两种方法。
你的是:保留小数点后N位的一种方法。
public static String round(double f, int n) {
n -= getOffset(f) + 1;
f *= Math.pow(10, n);
f = Math.round(f);
f /= Math.pow(10, n);
return format(f, n);
}
private static String format(double num, int precision){
String fN = "" + num;
boolean hasDot;
int index;
if((index = getOffset(num)) >= 0) hasDot = true;
else hasDot = false;
int len = fN.length();
for (int i = precision + 1;i >= len; i--){
if(hasDot) fN = fN + "0";
else fN = "0" + fN;
}
return fN;
}
private static int getOffset(double d){
String fN = "" + d;
return fN.indexOf(".");
}
public static String doubleToString(double d,int i){
Double f=new Double(d);
NumberFormat form;
form = (DecimalFormat)NumberFormat.getInstance();
form.setMaximumFractionDigits(i);
BigDecimal testBD=new BigDecimal(form.format(f)).setScale(i);//设置最后显示位数
return testBD.toString();
}
public static void main(String[] args){
System.out.println(round(26.1235123,5));
System.out.println(round(0.6,5));
System.out.println(round(12.2536, 5));
System.out.println(round(1.25656,5));
System.out.println(round(1.0, 5));
System.out.println(round(0.1,5));
}26.12
0.600
12.25
1.257
1.000
0.100