题解 | #取近似值#
取近似值
https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
方法一:double
转 int
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); double num = Double.parseDouble(br.readLine()); pw.println((int) (num + 0.5)); pw.flush(); pw.close(); br.close(); } }
方法二:字符串判断
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); String str = br.readLine(); int index = str.indexOf('.'); String substr = str.substring(0, index); if (str.charAt(index + 1) < '5') { pw.println(substr); } else { pw.println(Integer.parseInt(substr) + 1); } pw.flush(); pw.close(); br.close(); } }