如何设置浮点类型的精度?

猪小烧 2007-08-21 05:37:24
如何设置浮点类型的精度?

在JAVA里,不知道怎么去设置自己需要的浮点精度....................

求前辈指点

难道真的要转换为string再转回来?
...全文
341 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
猪小烧 2007-08-22
  • 打赏
  • 举报
回复
楼上的没看懂...............
APOLLO_TS 2007-08-21
  • 打赏
  • 举报
回复
A/1=A
B/1=B
C/1=C
看出门道来了吧!
将所得数除以new BigDecimal(1)就可以设置精度了,其中有5个主要的舍入方式。
基本够你用的!
就IBM也这么干的!
lip009 2007-08-21
  • 打赏
  • 举报
回复
public class Test {

public static void main(String args[]){
Test test=new Test();
System.out.println(test.setFloatPrecision(123.2563,"0.00"));
}

public String setFloatPrecision(double param,String format){
String result=null;

DecimalFormat d_format=new DecimalFormat(format);
result=d_format.format(param);

return result;
}
}
zdjray 2007-08-21
  • 打赏
  • 举报
回复
不过还是这样吧
import java.math.BigDecimal;
public class FloatFormatTest {

public static void main(String[] args) {
BigDecimal n = new BigDecimal("1234567.56789");
BigDecimal n2 = n.setScale(2, BigDecimal.ROUND_FLOOR);
System.out.println(n);
System.out.println(n2);
}
}

然后可以用BigDecimal的floatValue得到float
zdjray 2007-08-21
  • 打赏
  • 举报
回复
用Float类的
public static float intBitsToFloat(int bits)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧