Math.round()方法
当前位置:知识管理交流
→『 技术文档交流 』
Math.round()方法
java.lang.Math类里有两个round()方法,它们的定义如下: public static int round(float a) { //other code } public static long round(double a) { //other code } 它们的返回值都是整数,且都采用四舍五入法。运算规则如下:
package com.sdjt.study.jibenleixing; /** * @author:lyy * @version 创建时间:2009-8-4 下午06:33:28 类说明 */ 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)); } }
结果为: 小数点后第一位=5 小数点后第一位<5 小数点后第一位>5
最后结论: Math类的round()方法的运算结果是一个<=(参数值+0.5)的最大整数。 该文章在 2011/4/8 12:27:40 编辑过 |
相关文章
正在查询... |