码迷,mamicode.com
首页 > 编程语言 > 详细

java中截尾和舍入

时间:2017-05-08 20:00:34      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:java   使用   gnu   float   static   numbers   oid   style   util   

//p55 3.15.1截尾和舍入

import static util.Print.*;
public class CastingNumbers {

public static void main(String [] args){
double a=0.4,b=0.7;
float fa=0.4f,fb=0.7f;
print("(int)a:"+(int)a);

print("(int)b:"+(int)b);

print("(int)fa:"+(int)fa);

print("(int)fb:"+(int)fb);

}
}

运行结果:

(int)a:0
(int)b:0
(int)fa:0
(int)fb:0

//浮点型转换成整型小数点的取舍问题
//需要使用java.lang.Math中的round()方法
//不使用 小数点将全部舍去
import static util.Print.*;
public class CastingNumbers {

public static void main(String [] args){
double a=0.4,b=0.7;
float fa=0.4f,fb=0.7f;
print("Math.round(a):"+Math.round(a));
print("Math.round(b):"+Math.round(b));
print("Math.round(fa)"+Math.round(fa));
print("Math.round(fb):"+Math.round(fb));
}
}

运行结果:

Math.round(a):0
Math.round(b):1
Math.round(fa)0
Math.round(fb):1

 

java中截尾和舍入

标签:java   使用   gnu   float   static   numbers   oid   style   util   

原文地址:http://www.cnblogs.com/1234jingjing/p/6826897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!