数值操作之四舍五入
package com.think.in.java;
/**
* @author Xiaopeng
*
*/
public class CastingNumbers {
public static void main(String[] args){
double above = 0.7,below = 0.4;
float fabove = 0.7f,fbelow = 0.4f;
//直接截斷小數(shù)點后
System.out.println("(int)above"+ (int)above);
System.out.println("(int)below"+ (int)below);
System.out.println("(int)fabove"+ (int)fabove);
System.out.println("(int)fbelow"+ (int)fbelow);
//取四舍五入用Math.round
System.out.println("Math.round(above)"+ Math.round(above));
System.out.println("Math.round(below)"+ Math.round(below));
System.out.println("Math.round(fabove)"+ Math.round(fabove));
System.out.println("Math.round(fbelow)"+ Math.round(fbelow));
}
}
/**
* @author Xiaopeng
*
*/
public class CastingNumbers {
public static void main(String[] args){
double above = 0.7,below = 0.4;
float fabove = 0.7f,fbelow = 0.4f;
//直接截斷小數(shù)點后
System.out.println("(int)above"+ (int)above);
System.out.println("(int)below"+ (int)below);
System.out.println("(int)fabove"+ (int)fabove);
System.out.println("(int)fbelow"+ (int)fbelow);
//取四舍五入用Math.round
System.out.println("Math.round(above)"+ Math.round(above));
System.out.println("Math.round(below)"+ Math.round(below));
System.out.println("Math.round(fabove)"+ Math.round(fabove));
System.out.println("Math.round(fbelow)"+ Math.round(fbelow));
}
}
out put:?
(int)above0
(int)below0
(int)fabove0
(int)fbelow0
Math.round(above)1
Math.round(below)0
Math.round(fabove)1
Math.round(fbelow)0
?
—— —— —— EOF —— —— —— 作者: 畢小朋 |?http://www.cnblogs.com/bixiaopeng/ 微博: weibo.com/wirelessqa 轉載請注明來源: 畢小朋 —?http://www.cnblogs.com/bixiaopeng/轉載于:https://www.cnblogs.com/bixiaopeng/articles/2420929.html
總結
- 上一篇: 软件工厂是否真的可能存在?
- 下一篇: 如何在Jsp上传图片