题解 | #取近似值#
取近似值
http://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
解题思路:
就是将输入的double类型数据num先转换成int类型zhengshu,此时只保留整数部分,小数部分一律截断。然后将num*10 - zhengshu * 10得到的就是十分位小数的值,判断该值是否大于或等于5,如果是就需要zhengshu加1,表示向上取整,否则就是zhengshu,表示向下取整
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); double num = scan.nextDouble(); int zhengshu = (int)num; int xiaoshu = (int)(num * 10) - zhengshu * 10; if(xiaoshu >= 5) { zhengshu++; } System.out.println(zhengshu); } }