题解 | #取近似值#
取近似值
http://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
package com.company.niuke;
import java.util.Scanner;
/**
* @ProjectName: leetcode
* @Package: com.company.niuke
* @ClassName: JinSiZhi
* @Author: Administrator
* @Description: 近似值
* @Date: 2022/2/16 21:03
* @Version: 1.0
*/
public class JinSiZhi {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
Float num = scanner.nextFloat();
if ((num * 10) % 10 >= 5) {
System.out.println((int) Math.ceil(num)); //math.ceil()向上取整。
} else {
System.out.println((int) Math.round(num)); //math.round() 四舍五入
}
}catch (Exception e) {
throw new RuntimeException("类型转换出错",e);
}
}
}