题解 | #实现四舍五入#
实现四舍五入
http://www.nowcoder.com/practice/020a0cf673174d5795d97ae79cff59a0
方法一:利用java自己的api
public class Main{
public static void main(String [] args){
Scanner scanner=new Scanner(System.in);
float f=scanner.nextFloat();
System.out.print(Math.round(f));
}
}
方式二:利用强制类型转换 大于0的数据加0.5再进行强制类型转换
public class Main{
public static void main(String [] args){
Scanner scanner=new Scanner(System.in);
double f=scanner.nextDouble();
if(f>=0){
System.out.print((int)(f+0.5));
}else{
System.out.print((int)(f-0.5))}
}