Java四舍五入及保留小数点位数

本文介绍了Java中实现四舍五入的多种方法,包括Math类中的round和rint函数,以及如何使用DecimalFormat来精确控制小数点后的位数。

关于java的四舍五入函数,精确到小数点后n位的方法。

1. 四舍五入函数
- Math.round(double a)
- Math.rint(double a)

long java.lang.Math.round(double a)
Returns the closest long to the argument, with ties rounding to positive infinity.

Special cases:

If the argument is NaN, the result is 0.
If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

Parameters:

    a a floating-point value to be rounded to a long.

Returns:

the value of the argument rounded to the nearest long value.
Math.round(2.50);
运行结果:3
Math.round(2.49);
运行结果:2

double java.lang.Math.rint(double a)

Returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even. Special cases:

If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
Parameters:

a a double value.

Returns:

the closest floating-point value to a that is equal to a mathematical integer.
Math.rint(2.51)
运行结果:3.0
Math.rint(2.50);//此处特殊,2.50也认为是舍,而比2.50大,才能入。
运行结果:2.0
----------

2. 舍函数Math.floor(2.8)

double java.lang.Math.floor(double a)

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases:

If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
Parameters:

a a value.

Returns:

the largest (closest to positive infinity) floating-point value that less than or equal to the argument and is equal to a mathematical integer.
Math.floor(2.8)
运行结果:2.0
Math.floor(2.3)
运行结果:2.0

3. 保留n位小数(精确到n位小数)

DecimalFormat d = new DecimalFormat("#.0");
System.out.println(d.format(2.5563));
运行结果:2.6
"#.0"表示保留一位小数
"#.00"表示保留两位小数
"#.000"表示保留三位小数
"#.0000"表示保留四位小数
...
...
以此类推
### Java 中对 Double 类型进行四舍五入保留两位小数 在 Java 中,可以通过多种方式实现 `double` 类型数据的四舍五入操作,并将其保留到指定的小数位数。以下是几种常见的方法及其特点: #### 方法一:使用 `String.format()` 函数 通过调用 `String.format()` 方法可以轻松完成数值格式化工作。该方法支持自定义输出格式字符串,其中 `"%.2f"` 表示保留两位小数。 ```java double a = 2.100; double b = 2.104; double c = 2.105; String fig1 = String.format("%.2f", a); String fig2 = String.format("%.2f", b); String fig3 = String.format("%.2f", c); System.out.println(fig1); // 输出: 2.10 System.out.println(fig2); // 输出: 2.10 System.out.println(fig3); // 输出: 2.11 ``` 这种方法简单易懂,适用于仅需显示结果而无需进一步计算的情况[^1]。 --- #### 方法二:利用 `BigDecimal` 的 `setScale()` 方法 对于更精确的需求或者需要参与后续运算的数据处理场景,则推荐采用 `BigDecimal` 提供的功能来控制精度与舍入模式。 ```java import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double d = 3.1415926; BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_UP); System.out.println(bd.toString()); // 输出: 3.15 } } ``` 这里设置了保留两位小数 (`newScale=2`) 并选择了半进制向上取整策略(`RoundingMode.HALF_UP`)作为默认选项之一[^2]。这种方式不仅能够满足基本需求还能应对更加复杂的业务逻辑要求比如金融领域内的金额计算等场合下需要用到严格遵循特定规则下的近似值生成过程[^3]. --- #### 方法三:借助于 `DecimalFormat` 另一种可行的选择便是运用 `DecimalFormat`, 它允许开发者灵活定制数字呈现样式的同时也具备一定的自动调整能力以适应不同输入源所提供的原始资料特性差异. ```java import java.text.DecimalFormat; public class Main { public static void main(String[] args){ double num = 3.1415926; DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(); df.applyPattern("#.##"); String formattedNumber = df.format(num); System.out.println(formattedNumber); // 输出: 3.14 } } ``` 需要注意的是当应用此类技术手段时可能会遇到一些潜在陷阱例如千分位符号干扰等问题因此实际编码过程中应当谨慎对待每一个细节部分确保最终产物既美观又实用[^4]. --- ### 总结 以上三种方案各有优劣,在选择具体实施方案前应充分考虑项目背景及预期目标从而做出最合适的决定。如果只是单纯为了打印友好型的结果那么第一种基于字符串替换的办法无疑是最简便快捷的一种途径;而对于那些追求极致准确性并且可能涉及到复杂商业交易的应用程序来说第二种依靠大数库来进行精细化管理无疑是更好的解决方案最后一种虽然相对较为少见但仍不失为一个值得尝试的方向特别是在面对多语言环境或者是特殊文化习惯影响下的国际化开发工作中显得尤为重要。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子林Android

感谢老板,老板大气!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值