float保留一位小数
方法一:使用DecimalFormat类进行格式化
float num = 3.14159f;DecimalFormat decimalFormat = new DecimalFormat("#.0");String result = decimalFormat.format(num);float roundedNum = Float.parseFloat(result);System.out.println(roundedNum); // 输出:3.1
方法二:使用String类的format方法
float num = 3.14159f;String result = String.format("%.1f", num);float roundedNum = Float.parseFloat(result);System.out.println(roundedNum); // 输出:3.1
方法三:使用Math类的round方法
float num = 3.14159f;float roundedNum = Math.round(num * 10.0) / 10.0f;System.out.println(roundedNum); // 输出:3.1
3868

被折叠的 条评论
为什么被折叠?



