如何取小数点前两位并四舍五入

请解答如何来操作

[code="java"]java 取小数点前两位的4种办法
//取小数点的几种办法
class TestDot2
{
//方法1
public void test1(double c)
{
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
System.out.println(df.format(c));
}
//方法2
public void test2(double c)
{

java.math.BigDecimal bd = new java.math.BigDecimal(String.valueOf(c));

bd = bd.setScale(2,java.math.BigDecimal.ROUND_HALF_UP);

System.out.println(bd);
}
//方法3
public void test3(double c)
{
long l1 = Math.round(c*100); //四舍五入

double ret = l1/100.0; //注意:使用 100.0 而不是 100

System.out.println(ret);
}
//方法4
public void test4(double c)
{
c=((int)(c*100))/100.0;
System.out.println(c);
}
public static void main(String[] args)
{
double c = 3.056;
TestDot2 td2 = new TestDot2();
//td2.test1(c);
//td2.test2(c);
//td2.test3(c);
td2.test4(c);
}
}[/code]

先通过小数点 截取
12.67
先split 下。然后 判断split[1].substring(0) 也就是取小数点后第一个数字 后面的 第一位是否 >5 如果>5
split[0]+1 否则 split[0] 值 不变

大概这个思路
仅供参考

php:
[code]round(3.5674,2) --原型:round ( float val [, int precision])[/code]
javascript:
[code]
function ForDight(Dight,How)

{

Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);

return Dight;

}

[/code]

你是用哪种语言?

[url=http://oseye.net]开源视窗(osEye.net),面向新手、菜鸟的问答社区[/url]

java?javascript?actionscript?

很多语言里面都有round方法来四舍五入取小数点位数。。

www.google.com
http://cn.bing.com/
连搜索引擎都不用怎么出来混。。

赞成自己百度,谷歌!这个都不会 那你不要混了!

BigDecimal