java Math.round()
2012-03-06 21:13
531 查看
java Math.round()_李金峰_新浪博客
public class MathTest {
public static void
main(String[] args) {
System.out.println("小数点后第一位=5");
System.out.println("正数:Math.round(11.5)="
+ Math.round(11.5));
System.out.println("负数:Math.round(-11.5)="
+ Math.round(-11.5));
System.out.println();
System.out.println("小数点后第一位<5");
System.out.println("正数:Math.round(11.46)="
+ Math.round(11.46));
System.out.println("负数:Math.round(-11.46)="
+ Math.round(-11.46));
System.out.println();
System.out.println("小数点后第一位>5");
System.out.println("正数:Math.round(11.68)="
+ Math.round(11.68));
System.out.println("负数:Math.round(-11.68)="
+ Math.round(-11.68));
}
}
运行结果:
1、小数点后第一位=5
2、正数:Math.round(11.5)=12
3、负数:Math.round(-11.5)=-11
4、
5、小数点后第一位<5
6、正数:Math.round(11.46)=11
7、负数:Math.round(-11.46)=-11
8、
9、小数点后第一位>5
10、正数:Math.round(11.68)=12
11、负数:Math.round(-11.68)=-12
根据上面例子的运行结果,我们还可以按照如下方式总结,或许更加容易记忆:
1、参数的小数点后第一位<5,运算结果为参数整数部分。
2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。
3、参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。
终结:大于五全部加,等于五正数加,小于五全不加。
java Math.round()
public static void
main(String[] args) {
System.out.println("小数点后第一位=5");
System.out.println("正数:Math.round(11.5)="
+ Math.round(11.5));
System.out.println("负数:Math.round(-11.5)="
+ Math.round(-11.5));
System.out.println();
System.out.println("小数点后第一位<5");
System.out.println("正数:Math.round(11.46)="
+ Math.round(11.46));
System.out.println("负数:Math.round(-11.46)="
+ Math.round(-11.46));
System.out.println();
System.out.println("小数点后第一位>5");
System.out.println("正数:Math.round(11.68)="
+ Math.round(11.68));
System.out.println("负数:Math.round(-11.68)="
+ Math.round(-11.68));
}
}
运行结果:
1、小数点后第一位=5
2、正数:Math.round(11.5)=12
3、负数:Math.round(-11.5)=-11
4、
5、小数点后第一位<5
6、正数:Math.round(11.46)=11
7、负数:Math.round(-11.46)=-11
8、
9、小数点后第一位>5
10、正数:Math.round(11.68)=12
11、负数:Math.round(-11.68)=-12
根据上面例子的运行结果,我们还可以按照如下方式总结,或许更加容易记忆:
1、参数的小数点后第一位<5,运算结果为参数整数部分。
2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。
3、参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。
终结:大于五全部加,等于五正数加,小于五全不加。
相关文章推荐
- 关于 java 中负数四舍五入的问题——Math.round()
- Java Math的 floor,round和ceil的总结
- java Math.round() Java中的四舍五入函数
- Java Math floor round ceil 函数
- Java Math的 floor,round和ceil的总结
- Java Math.round()函数小结
- Java中Math.round()
- Java:Math.round
- Java Math的 floor,round和ceil的总结
- 【JAVA】Math.Round()函数常见问题“四舍5入”
- 马士兵Java课堂笔记-4-Math.round()函数-强制转换中可以实现四舍五入
- Java Math的 floor,round和ceil的总结
- [java] Math.round()
- 关于java中的Math.round、floor、ceil方法区别
- Java Math.round()函数小结
- Java中Math.round()
- Java Math.round()
- java.Math.round()
- Java Math的 floor,round和ceil的总结
- Java Math的floor,round,ceil函数小结