JSP 中如何设置 float 数 的精度?

sunthing 2003-08-20 04:46:43
字符串 15.3265 转成float 后,我想得到小数点后只保存2位的 float 15.33
请问用什么函数?
...全文
522 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunthing 2003-08-20
  • 打赏
  • 举报
回复
太好了,谢谢大家!
只要小数点两位的话 diewikwang(coconut) ,lgs6715(),sasa_sasa(乡巴佬) 的最简单
huanxiangzhe24(huanxiangzhe24) 的是比较好
cui_yihao(pg) 这个人比较有钻的精神,


<%@page import="java.math.*"%>
<%
double aa=15.3265;
double bb;
double cc;

bb=aa*100;
cc=(int)Math.round(bb)/100.00;
out.println ("the result is :" + cc+"<br>"); //15.33


double a=82.6984;
BigDecimal b=new BigDecimal(a);
BigDecimal c=b.setScale(3,BigDecimal.ROUND_HALF_UP);//82.698
BigDecimal d=b.setScale(2,BigDecimal.ROUND_HALF_UP); //82.70
BigDecimal e=b.setScale(1,BigDecimal.ROUND_HALF_UP); //82.7
BigDecimal f=b.setScale(0,BigDecimal.ROUND_HALF_UP); //83
out.println ("the result is :"+c+"<br>");
out.println ("the result is :"+d+"<br>");
out.println ("the result is :"+e+"<br>");
out.println ("the result is :"+f+"<br>");
%>

好了,大家给了我这么多的方法,给了我这么多的鼓励,我加分来谢谢大家
来者有份!同时为了让更多需要这种帮助的人能得到大家的帮助,所以暂不结贴,
但这贴以后的发贴的同志,将不会有分了,不便之处,敬请谅解!
treeClimber 2003-08-20
  • 打赏
  • 举报
回复
别考虑太复杂了,如果你仅仅想要前两位,可以format一下,相关类是:java.util.NumberFormt,java.util.DecimalFormt
cui_yihao 2003-08-20
  • 打赏
  • 举报
回复
/**
* 四舍五入
* @param f double 类型,要进行四舍五入的数字
* @param position 要保留小数位的个数
* @return String
* @throws Exception
*/
public String round (double f,int position) throws Exception {
String sf = String.valueOf(f);
int i = sf.indexOf(".");
String s1 = "" ,s2 = "";
if(sf.substring(i+1).length() <= position){
return String.valueOf(f);
}
if(position == 0){
s1 = sf.substring(i-1,i);
s2 = sf.substring(i+position+1,i+position+2);
if(Integer.parseInt(s2) >=5 ){
s1 = String.valueOf(Integer.parseInt(s1)+1);
}
return sf.substring(0,i-1)+s1;
}else{
s1 = sf.substring(i+position, i+position+1);
s2 = sf.substring(i+position+1,i+position+2);

if(Integer.parseInt(s2) >=5 ){
s1 = String.valueOf(Integer.parseInt(s1)+1);
}
return sf.substring(0,i+position)+s1;
}

}
sasa_sasa 2003-08-20
  • 打赏
  • 举报
回复
double x = 15.3265;
double y;
y = (int)Math.round(x * 100)/100.00;
out.println("the result is :"+ y);
lgs6715 2003-08-20
  • 打赏
  • 举报
回复
double aa=15.3265;
double bb;
double cc;

bb=aa*100;
cc= (int)Math.round(bb)/100.0 ;
out.println ("the result is :" + cc+"<br>"); //15.33
diewikwang 2003-08-20
  • 打赏
  • 举报
回复
或者
aa = Math.round(aa*100)/100.00
huanxiangzhe24 2003-08-20
  • 打赏
  • 举报
回复
float a=5.3445;
BigDecimal b=new BigDecimal(a);
BigDecimal c=b.setScale(2,BigDecimal.ROUND_HALF_UP))//四舍五入保留二位;
a=c.floatValue();

除了四舍五入外,还有截断,五舍六入等保留方法。
不过要import java.math.*
diewikwang 2003-08-20
  • 打赏
  • 举报
回复

double aa = 123.23232323;
int bb = (int)(aa * 100);
System.out.println(bb/100.00);
sunthing 2003-08-20
  • 打赏
  • 举报
回复
double aa=15.3265;
double bb;

bb=(aa*100)/100.0;
System.out.println(String.valueOf(bb));

But the result is 15.3265 still
sunthing 2003-08-20
  • 打赏
  • 举报
回复
double aa=15.3265;
double bb;

bb=(aa*100)/100.0;
System.out.println(String.valueOf(bb));

But the result is 15.3265 still
mailguoyy 2003-08-20
  • 打赏
  • 举报
回复
temp1= String.valueOf(Float.parseFloat(totalMark)/totalMark2*100);
dotI = temp1.indexOf(".");
tempLen = temp1.length();
if (dotI+3<=tempLen){tempLen =dotI+3;}
temp1=temp1.substring(0,tempLen);

不知有没有好的方法
lgs6715 2003-08-20
  • 打赏
  • 举报
回复
四舍五入总会吧
我是菜鸟一个,但写写还可以的

(int)Math.ceil((double)Integer.parseInt(num1)/50)

15.3265可以先×100
后/100.0
sunthing 2003-08-20
  • 打赏
  • 举报
回复
不会吧,没人理我?

81,095

社区成员

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

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