在Java中如何将双精度数截断为仅保留两位小数?
例如,我有变量 3.545555555,我想将其截断为 3.54。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
例如,我有变量 3.545555555,我想将其截断为 3.54。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(19)
我一直在寻找一个两位小数随机数生成器,无论它是浮点数还是双精度数,我真的不介意。我从浮点数开始,最后返回双精度数。在 @Rainbow979 上面的代码的帮助下,稍加修改,它就完美地工作了。我使用 Math.floor 代替 Math.round。
感谢@Rainbow979
输出:1088.48
(当我使用随机数生成时,每次执行都会发生变化)
I was looking for a two decimal random number generator whether it is a float or double, i really don't mind. I started with floating number and ended returning double. With the help of the code above from @Rainbow979 with minor changes it worked perfectly. Instead of Math.round, I used Math.floor.
thanks to @Rainbow979
Output: 1088.48
(changes for every execution as I used randon number generation)
这对我有用:
我个人喜欢这个版本,因为它实际上以数字方式执行舍入,而不是通过将其转换为字符串(或类似的)然后对其进行格式化。
This worked for me:
I personally like this version because it actually performs the rounding numerically, rather than by converting it to a String (or similar) and then formatting it.
如果您希望将其用于显示目的,请使用
java .text.DecimalFormat
:如果需要它进行计算,请使用
java.lang.Math
:If you want that for display purposes, use
java.text.DecimalFormat
:If you need it for calculations, use
java.lang.Math
:检查可用的
RoundingMode
和DecimalFormat
。Check available
RoundingMode
andDecimalFormat
.其他答案都不适用于正值和负值(我的意思是用于计算并且只是在不进行舍入的情况下进行“截断”)。并且不转换为字符串。
从如何将数字四舍五入到小数点n Java 中的地方 link
这种方法对我来说效果很好。
结果 :
None of the other answers worked for both positive and negative values ( I mean for the calculation and just to do "truncate" without Rounding). and without converting to string.
From the How to round a number to n decimal places in Java link
This method worked fine for me .
Results :
首先请注意,
double
是一个二进制分数,并且实际上没有 小数位。如果需要小数位,请使用
BigDecimal
,它具有用于截断的setScale()
方法,或使用DecimalFormat
获取字符串
。Note first that a
double
is a binary fraction and does not really have decimal places.If you need decimal places, use a
BigDecimal
, which has asetScale()
method for truncation, or useDecimalFormat
to get aString
.格式化为字符串并转换回双精度我认为会给你你想要的结果。
double 值不会是 round()、floor() 或 ceil()。
一个快速解决方法可能是:
您可以使用 sValue 进行显示,或使用 newValue 进行计算。
Formating as a string and converting back to double i think will give you the result you want.
The double value will not be round(), floor() or ceil().
A quick fix for it could be:
You can use the sValue for display purposes or the newValue for calculation.
如果出于某种原因,您不想使用 BigDecimal,您可以将 double 转换为 int 来截断它。
如果要截断到 个位 位置:
int
到 十位 位置:
int< /code>
double
Hundreths 个位置
示例:
在此示例中,
decimalPlaces
是您想要去的位置之后的位置数,因此 1 将四舍五入为十分位,2到百分位,依此类推(0舍入到个位,负一到十位, ETC。)If, for whatever reason, you don't want to use a
BigDecimal
you can cast yourdouble
to anint
to truncate it.If you want to truncate to the Ones place:
int
To the Tenths place:
int
double
Hundreths place
Example:
In this example,
decimalPlaces
would be the number of places PAST the ones place you wish to go, so 1 would round to the tenths place, 2 to the hundredths, and so on (0 rounds to the ones place, and negative one to the tens, etc.)您可以使用 NumberFormat 类对象来完成该任务。
You can use NumberFormat Class object to accomplish the task.
3.545555555 得到 3.54。
尝试以下操作:
这将给出 = 3.54!
3.545555555 to get 3.54.
Try Following for this:
This will give= 3.54!
也许是 Math.floor(value * 100) / 100 ?请注意,像
3.54
这样的值可能无法用double
精确表示。Maybe
Math.floor(value * 100) / 100
? Beware that the values like3.54
may be not exactly represented with adouble
.这是我使用的方法:
也可以这样做:
Here is the method I use:
This can also be done:
我使用 Math.floor() 方法和小数点基本移动 (100 = 2)。
I used Math.floor() method and basic moving of decimal places by (100 = 2).
快速检查是使用 Math.floor 方法。我创建了一种方法来检查双精度数是否有两位或更少的小数位,如下:
A quick check is to use the Math.floor method. I created a method to check a double for two or less decimal places below:
在此解决方案中,这将截断双精度数,仅保留两位小数。此解决方案将不会对双精度值进行舍入。
这将输出3.54
DecimalFormat("#.##") - 在这里,我在小数点后输入了两个哈希符号(##) 。因此,这会将数字截断至小数点后两位。
这对积极和消极的人都有效。负值。
In this solution, this will TRUNCATE a double to only two decimal places. This solution will not ROUND OFF the double value.
This will output 3.54
DecimalFormat("#.##") - Here, I entered two hash symbols(##) after the decimal point. Hence, this will truncate the number up to two decimal places.
This will work for both Positive & Negative values.
也许如下:
Maybe following :
我有一个稍微修改过的 Mani 版本。
输出:
I have a slightly modified version of Mani's.
Output:
双第一值=-3.1756d;
双值1 = (((int)(Math.pow(10,3)*firstValue))/Math.pow(10,3));
double firstValue = -3.1756d;
double value1 = (((int)(Math.pow(10,3)*firstValue))/Math.pow(10,3));