可以使用 Math.floor() 方法将字符串转换为 double 类型,再进行向下取整操作。示例如下:

String str = '3.5';
double num = Double.parseDouble(str);
int result = (int) Math.floor(num); // 向下取整
System.out.println(result); // 输出 3

需要注意的是,向下取整后得到的结果为 int 类型,如果需要转换为字符串,可以使用 String.valueOf() 方法。

Java 字符串类型向下取整到 0.5 为单位

原文地址: https://www.cveoy.top/t/topic/oFk9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录