小數點問題? 我用Float類來處理小數點問題.我要得到最後數值是有固定位數的小數點.有多就四捨五入.有少就不用處理,不知道在JAVA中如何處理呢?比如:5.02356這個數我只要三位,如何處理呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.text.DecimalFormat;import java.text.NumberFormat;public class Test{ public static void main(String[] args) { Float f=new Float(5.02356); float a=(float)0.0; NumberFormat form; form = (DecimalFormat)NumberFormat.getInstance(); form.setMaximumFractionDigits(4);//设置小数点的位数 try{ a=form.parse(form.format(f)).floatValue(); }catch(Exception e){ System.out.println(e.getMessage()); } System.out.println("After format f is: "+a); }} 刚才的有点绕圈了,下面的更直观,简洁!import java.text.DecimalFormat;import java.text.NumberFormat;public class Test{ public static void main(String[] args) { Float f=new Float(5.02356); float a=(float)0.0; NumberFormat form; form = (DecimalFormat)NumberFormat.getInstance(); form.setMaximumFractionDigits(3);//设置小数点的位数 f=Float.valueOf(form.format(f)); System.out.println("After format f is: "+f); }} 楼主,我写了一个,但比较麻烦,希望抛砖引玉!public class InterceptDecimal { //sourceFloat 源Float型,index保留的小数位数 public static float getFloat(Float sourceFloat,int index){ char dot = '.'; String sourceString=""; int dotIndex;//小数点位置 sourceString =sourceFloat.toString(); dotIndex = sourceString.indexOf(dot); //整数和小数部分 String sourceNum,sourceDot; sourceNum = sourceString.substring(0,dotIndex); sourceDot = sourceString.substring(dotIndex+1); boolean blog = false;//进位标志 //# //System.out.println("小数后一位"+Float.parseFloat(sourceDot.substring(index,index+1))); //取出要取小数后第一位判断是否大于4 if(Float.parseFloat(sourceDot.substring(index,index+1))>4){ blog = true; } if(blog){ //取小数 sourceString = sourceNum+sourceDot.substring(0,index); //加一 sourceString = Float.toString(Float.parseFloat(sourceString)+1); //加小数点 sourceString = sourceString.substring(0,dotIndex)+"."+sourceString.substring(dotIndex); //可能产生多余的. sourceString = sourceString.substring(0,dotIndex+index+1); }else{ //取小数 sourceString = sourceNum+sourceDot.substring(0,index); //加小数点 sourceString = sourceString.substring(0,dotIndex)+"."+sourceString.substring(dotIndex); } return Float.parseFloat(sourceString); } public static void main(String[] args){ Float s = new Float(2332.36567); int index = 2; System.out.println(InterceptDecimal.getFloat(s,index)); }} chylwk(沧海一浪) 厉害! 没有必要使用格式化吧,使用BigDecimal好过啦BigDecimal bd = new BigDecimal(2332.36567);bd = bd.setScale(3 , BigDecimal.ROUND_HALF_UP);return bd.doubleValue();后面那个我记得应该是指四舍五入的,可以看看文档说明 public static double formatDec(float f, int n) { //f为传进的浮点数,n为楼主希望的小数位数 double f1; if (!java.lang.Float.isNaN(f)) { f1 = f; } else { f1 = 0; } try { String decPatter = "0"; ; if (n > 0) { decPatter = "0."; for (int i = 0; i < n; i++) { decPatter += "0"; } } NumberFormat nf = new DecimalFormat(decPatter); String s = nf.format(f); f1 = Float.parseFloat(s); } catch (Exception ex) { f1 = f; } return f1; } public static float formatDec(float f, int n) { //f为传进的浮点数,n为楼主希望的小数位数 float f1; if (!java.lang.Float.isNaN(f)) { f1 = f; } else { f1 = 0; } try { String decPatter = "0"; ; if (n > 0) { decPatter = "0."; for (int i = 0; i < n; i++) { decPatter += "0"; } } NumberFormat nf = new DecimalFormat(decPatter); String s = nf.format(f); f1 = Float.parseFloat(s); } catch (Exception ex) { f1 = f; } return f1; } 呵呵,上面是我以前做的截取double小数点位数的,刚才拿来就用,居然还带有double的痕迹 请教问题:关于Date格式化 北大acm 1002 求救 netbeans-6.0.1安装问题 比较JAVA /DELPHI / POWER BUILDER / VC++ / C# 的前途 菜鸟问题,,让您劳神了! 做出来500分 跪求:java怎么实现打开一个EXE文件? 如何用java实现打印的横向分页?急求! 执行ant命令的错误,谁遇到过? 一个没看明白的do while java分析数据中的数据的问题很急!!!!!!!!!(高手请进。。。。) 从文件读取字符串的比较,急急急,在线等
import java.text.NumberFormat;public class Test{
public static void main(String[] args) {
Float f=new Float(5.02356);
float a=(float)0.0;
NumberFormat form;
form = (DecimalFormat)NumberFormat.getInstance();
form.setMaximumFractionDigits(4);//设置小数点的位数
try{
a=form.parse(form.format(f)).floatValue();
}catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("After format f is: "+a);
}
}
import java.text.NumberFormat;public class Test{
public static void main(String[] args) {
Float f=new Float(5.02356);
float a=(float)0.0;
NumberFormat form;
form = (DecimalFormat)NumberFormat.getInstance();
form.setMaximumFractionDigits(3);//设置小数点的位数
f=Float.valueOf(form.format(f));
System.out.println("After format f is: "+f);
}
}
public class InterceptDecimal {
//sourceFloat 源Float型,index保留的小数位数
public static float getFloat(Float sourceFloat,int index){
char dot = '.';
String sourceString="";
int dotIndex;//小数点位置
sourceString =sourceFloat.toString();
dotIndex = sourceString.indexOf(dot);
//整数和小数部分
String sourceNum,sourceDot;
sourceNum = sourceString.substring(0,dotIndex);
sourceDot = sourceString.substring(dotIndex+1);
boolean blog = false;//进位标志
//#
//System.out.println("小数后一位"+Float.parseFloat(sourceDot.substring(index,index+1)));
//取出要取小数后第一位判断是否大于4
if(Float.parseFloat(sourceDot.substring(index,index+1))>4){
blog = true;
}
if(blog){
//取小数
sourceString = sourceNum+sourceDot.substring(0,index);
//加一
sourceString = Float.toString(Float.parseFloat(sourceString)+1);
//加小数点
sourceString = sourceString.substring(0,dotIndex)+"."+sourceString.substring(dotIndex);
//可能产生多余的.
sourceString = sourceString.substring(0,dotIndex+index+1);
}else{
//取小数
sourceString = sourceNum+sourceDot.substring(0,index);
//加小数点
sourceString = sourceString.substring(0,dotIndex)+"."+sourceString.substring(dotIndex);
}
return Float.parseFloat(sourceString);
}
public static void main(String[] args){
Float s = new Float(2332.36567);
int index = 2;
System.out.println(InterceptDecimal.getFloat(s,index));
}
}
bd = bd.setScale(3 , BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();后面那个我记得应该是指四舍五入的,可以看看文档说明
double f1;
if (!java.lang.Float.isNaN(f)) {
f1 = f;
}
else {
f1 = 0;
} try {
String decPatter = "0"; ;
if (n > 0) {
decPatter = "0.";
for (int i = 0; i < n; i++) {
decPatter += "0";
}
}
NumberFormat nf = new DecimalFormat(decPatter);
String s = nf.format(f);
f1 = Float.parseFloat(s);
}
catch (Exception ex) {
f1 = f;
}
return f1;
}
float f1;
if (!java.lang.Float.isNaN(f)) {
f1 = f;
}
else {
f1 = 0;
} try {
String decPatter = "0"; ;
if (n > 0) {
decPatter = "0.";
for (int i = 0; i < n; i++) {
decPatter += "0";
}
}
NumberFormat nf = new DecimalFormat(decPatter);
String s = nf.format(f);
f1 = Float.parseFloat(s);
}
catch (Exception ex) {
f1 = f;
}
return f1;
}