Double、float类型精确到小数点后几位
2016-12-01 09:34
489 查看
需求:返回的一系列数据,精确到小数点后2位
方法一、使用Math.round()
Double value = 0.254668;
(double)Math.round(value*100)/100
方法二、使用DecimalFormat方法
DecimalFormat format=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
String p=format.format(value);//format 返回的是字符串
我当前的需求是计算两个坐标的距离,小于1000米显示米,大于一公里显示公里
double distance = .....;
if(distance>1000){
//方法一
DecimalFormat format=new DecimalFormat(".0");//不足1位,会以0补足.
String v=format.format(distance/1000);
Log.i("distance", "DecimalFormat: "+v+"公里");
//方法二
double a =(double)Math.round(distance/100)/10;
Log.i("distance", "Math.round: "+a+"公里");
}else{
Log.i("distance", "markPoint: "+(int)distance+"米");
}
方法一、使用Math.round()
Double value = 0.254668;
(double)Math.round(value*100)/100
方法二、使用DecimalFormat方法
DecimalFormat format=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
String p=format.format(value);//format 返回的是字符串
我当前的需求是计算两个坐标的距离,小于1000米显示米,大于一公里显示公里
double distance = .....;
if(distance>1000){
//方法一
DecimalFormat format=new DecimalFormat(".0");//不足1位,会以0补足.
String v=format.format(distance/1000);
Log.i("distance", "DecimalFormat: "+v+"公里");
//方法二
double a =(double)Math.round(distance/100)/10;
Log.i("distance", "Math.round: "+a+"公里");
}else{
Log.i("distance", "markPoint: "+(int)distance+"米");
}
相关文章推荐
- java float类型保留小数点后几位
- double或者float类型数据怎样保留小数点
- [读书笔记][Effective Java]不要在精确计算中使用float和double类型
- JAVA float double数据类型保留2位小数点5种方法
- Java中double类型的数据精确到小数点后两位
- Java中double类型的数据精确到小数点后两位
- Float,double类型数据保留小数位后几位
- 精确运算不能用float/double,用什么类型?
- 不要在精确计算中使用float和double类型
- java float double类型做小数点加减运算不准确
- 不要在精确计算中使用float和double类型
- java float double类型做小数点加减运算不准确
- double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较
- Java中double类型的数据精确到小数点后两位
- double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较
- ios 网上找了一个float类型四舍五入精确小数点的方法
- JAVA中控制double类型小数点后位数的方法
- float 和 double类型范围与精度
- 如果需要精确答案,请避免使用float和double