Java Math.round()函数小结



Java Math.round()函数小结

Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应,例如:ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.6)的结果为-11;floor的英文是地板,该方法就表示向下取整,Math.floor(11.6)的结果是11,Math.floor(-11.4)的结果-12;最难掌握的是round方法,他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果是12,Math.round(-11.5)的结果为-11.Math.round( )符合这样的规律:小数点后大于5全部加,等于5正数加,小于5全不加。

让我们看看JDK的说明:   
(1)public static long round(double a)   
returns the closest long to the argument. the result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. in other words, the result is equal to the value of the expression:       
       
    (long)math.floor(a    0.5d)   

(2)public static double floor(double a)   
    returns the largest(closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical  integer.special cases:
    if the argument value is already equal to a mathematical integer, then the result is the  same as the argument.       
    if the argument is nan or an infinity or positive zero or negative zero, then the result  is the same as the argument.   
       
    parameters:   
    a - a value.       
    returns:   
    the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer.

//import java.math.*;

public class RoundTest {

 

 

public static void main(String[] args) {

// TODO Auto-generated method stub

// Math.round():Java中的四舍五入函数

System.out.println("Case1:小数点后第一位 = 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("Case2:小数点后第一位 < 5");

System.out.println("正数:Math.round(11.49) = " + Math.round(11.49));

System.out.println("负数:Math.round(-11.49) = " + Math.round(-11.49));

 

System.out.println("Case3:小数点后第一位 > 5");

System.out.println("正数:Math.round(11.69) = " + Math.round(11.69));

System.out.println("负数:Math.round(-11.69) = " + Math.round(-11.69));

 

System.out.println("结论:正数小数点后大于5则进位;负数小数点后小于以及等于5都舍去,大于5的则进位");

System.out.println("也就是说:小数点后大于5全部加,等于5正数加,小于5全不加");

}

}

Math.round函数Math类中的一个方法,用于对一个数字进行四舍五入取整操作。根据引用\[1\]和引用\[2\]的解释,Math.round函数的工作原理是将原来的数字加上0.5后再向下取整。当小数点后第一位为5时,正数会向上取整,负数会向下取整。当小数点后第一位小于5时,正数和负数都会向下取整。当小数点后第一位大于5时,正数会向上取整,负数会向下取整。根据引用\[3\]中的代码示例,可以更清晰地理解Math.round函数的工作方式。例如,Math.round(0.4)的结果是0,Math.round(0.5)的结果是1,Math.round(-0.4)的结果是0,Math.round(-0.5)的结果是0。 #### 引用[.reference_title] - *1* *2* [Math.round()方法](https://blog.csdn.net/zhoushumin157016/article/details/51984436)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Math.round()和Math.random()的使用方法和思路](https://blog.csdn.net/weixin_47269259/article/details/121698620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值