//两数相除,保留四位有效数字
function divide(x,y){
var a = parseFloat(x); // 它不会报错,参见w3cshool:http://www.w3school.com.cn/js/jsref_parseFloat.asp
var b = parseFloat(y);
var c = 0;
if(y==0){
return c;
}
if (isNaN(a)||isNaN(b)){
return c;
}
c = Math.round((a/b)*10000)/10000;
return c;
}
----java
float num= (float)1/3;
System.out.println(num);
DecimalFormat df = new DecimalFormat("0.0000");//格式化小数
String s = df.format(num);//返回的是String类型
System.out.println(s); //0.3333