JAVA数值四舍五入

JAVA数值四舍五入 

Math.ceil求最小的整数但不小于本身. 

Math.round求本身的四舍五入。 

Math.floor求最大的整数但不大于本身. 

问题 

我要进行四舍五入或取近似值. 

解决办法 

用 Math.round( ) 进行四舍五入, Math.floor( ) 和 Math.ceil( ) 进行上下近似值。NumberUtilities.round( ) 方法可自定义取值。 

讨论 

很多情况我们需要得到整数部分而不是带有小数的浮点数。比如计算出结果为 3.9999999 ,期望的结果应该是4.0。 

Math.round( ) 方法进行四舍五入计算: 

trace(Math.round(204.499)); // 显示: 204 

trace(Math.round(401.5)); // 显示: 402 

Math.floor( ) 方法去掉小数部分,Math.ceil( ) 方法去掉小数部分后自动加1: 

trace(Math.floor(204.99)); // 显示: 204 

trace(Math.ceil(401.01)); // 显示: 402 

如果我想要把90.337 四舍五入到 90.34,可以这么写: 

trace (Math.round(90.337 / .01) * .01); //显示: 9.34 

trace (Math.round(92.5 / 5) * 5); // 显示: 95 

trace (Math.round(92.5 / 10) * 10); // 显示: 90 

更好的办法是用自定义函数NumberUtilities.round( ) ,它需要两个参数: 

number :要舍入的数字 

roundToInterval :间隔值 

NumberUtilities 类在 ascb.util 包中。 

imported ascb.util.NumberUtilities导入 

trace(NumberUtilities.round(Math.PI)); // Displays: 3 

trace(NumberUtilities.round(Math.PI, .01)); // Displays: 3.14 

trace(NumberUtilities.round(Math.PI, .0001)); // Displays: 3.1416 

trace(NumberUtilities.round(123.456, 1)); // Displays: 123 

trace(NumberUtilities.round(123.456, 6)); // Displays: 126 

trace(NumberUtilities.round(123.456, .01)); // Displays: 123.46 

### Java 中实现四舍五入的方法 在 Java 编程语言中,有多种方式可以实现数值四舍五入操作。以下是几种常见的方式及其具体实现: #### 1. **使用 `Math.round()` 方法** `Math.round()` 是一种简单且高效的方式来对浮点进行四舍五入处理。该方法会将浮点转换为最近的整数值[^1]。 对于正和负的情况,其行为如下: - 正:当小部分大于等于 0.5 时向上取整;小于 0.5 则向下取整。 - 负:遵循类似的规则,但需要注意边界情况(如 `-10.5` 的结果仍为 `-10`)。 示例代码如下: ```java public class MathRoundExample { public static void main(String[] args) { System.out.println(Math.round(10.4)); // 输出: 10 System.out.println(Math.round(10.5)); // 输出: 11 System.out.println(Math.round(-10.4)); // 输出: -10 System.out.println(Math.round(-10.5)); // 输出: -10 } } ``` 如果需要保留指定的小,则可以通过调整数值大小来间接完成此目标[^2]。例如: ```java public class DecimalRounding { public static void main(String[] args) { double num = 3.14159; int decimalPlaces = 2; // 需要保留的小 double roundedNum = Math.round(num * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces); System.out.println(roundedNum); // 输出: 3.14 } } ``` --- #### 2. **利用 `BigDecimal` 类** `BigDecimal` 提供了一种更灵活且精确的方式来控制数值的舍入模式。通过设置不同的舍入策略,开发者可以根据需自定义四舍五入的行为[^3]。 下面是一个基于 `BigDecimal` 实现四舍五入的例子: ```java import java.math.BigDecimal; public class BigDecimalExample { public static void main(String[] args) { BigDecimal bd = new BigDecimal("-2.5"); BigDecimal roundedValue = bd.setScale(0, BigDecimal.ROUND_HALF_UP); System.out.println("四舍五入后的值:" + roundedValue.toString()); // 输出: -2 } } ``` 在此例子中,`setScale(int scale, RoundingMode mode)` 方法用于指定保留的有效位以及采用何种舍入算法。其中 `ROUND_HALF_UP` 表示标准意义上的四舍五入逻辑。 --- #### 3. **借助字符串格式化工具 (`String.format`)** 除了上述两种技术外,还可以运用 `String.format` 函快速生成经过四舍五入处理过的字符串表示形式的据[^4]。这种方式特别适合于仅需展示最终结果而无需进一步计算的情形下应用。 实例演示如下所示: ```java public class FormatExample { public static void main(String[] args) { double num = 3.1415926; String formattedResult = String.format("%.4f", num); System.out.println(formattedResult); // 输出: 3.1416 } } ``` 这里 `%.<precision>f` 占位符中的 `<precision>` 定义了期望保留的小点后几位字长度。 --- ### 总结 综上所述,在实际开发过程中可根据具体情况选择合适的方案来进行据的四舍五入运算。每种方法都有各自的适用场景与优缺点,合理选用能够有效提升程序性能并满足业务需
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值