double a = 2.1266 现在只取到第3位,不要四舍五入,a = 2.126
怎么实现
------解决方案--------------------public static void main(String[] args) {
double a = 2.1266 ; // 现在只取到第3位,不要四舍五入,a = 2.126
String aStr = String.valueOf(a) ;
String aStr2 = aStr.substring(0, 5) ;
double aRst = Double.parseDouble(aStr2) ;
System.out.println(aRst) ;
}
结果:
2.126
------解决方案-------------------- public static void main(String[] args)
{
double a = 2.1266 ;
String as = String.valueOf(a) ;
String arr[] = as.split( ". ");
String a2 = arr[1].substring(0, 3) ;
double a0 = Double.parseDouble(arr[1]+ ". "+arr[2]) ;
System.out.println(a0) ;
}
}