在 Java ME 中将双精度舍入到小数点后 5 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1451149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding a double to 5 decimal places in Java ME
提问by Kevin Boyd
How do I round a double to 5 decimal places, without using DecimalFormat
?
如何在不使用的情况下将双精度舍入到小数点后 5 位DecimalFormat
?
采纳答案by Joren
You can round to the fifth decimal place by making it the first decimal place by multiplying your number. Then do normal rounding, and make it the fifth decimal place again.
您可以通过乘以您的数字使其成为第一个小数位,从而四舍五入到小数点后第五位。然后进行正常的四舍五入,并再次将其设为小数点后第五位。
Let's say the value to round is a double
named x
:
假设要舍入的值是一个double
命名的x
:
double factor = 1e5; // = 1 * 10^5 = 100000.
double result = Math.round(x * factor) / factor;
If you want to round to 6 decimal places, let factor
be 1e6
, and so on.
如果您想四舍五入到 6 位小数,让factor
be 1e6
,依此类推。
回答by Zed
If you are okay with external libraries, you can have a look at microfloat, specifically MicroDouble.toString(double d, int length).
如果你对外部库没问题,你可以看看microfloat,特别是MicroDouble.toString(double d, int length)。
回答by Jon Skeet
Whatever you do, if you end up with a double
value it's unlikely to be exactly5 decimal places. That just isn't the way binary floating point arithmetic works. The best you'll do is "the double value closestto the original value rounded to 5 decimal places". If you were to print out the exactvalue of that double, it would still probably have more than 5 decimal places.
不管你做什么,如果你最终得到一个double
值,它不太可能正好是小数点后 5 位。那不是二进制浮点运算的工作方式。最好的做法是“最接近原始值的双精度值四舍五入到小数点后 5 位”。如果您要打印出该双精度值的确切值,它的小数位仍可能超过 5 位。
If you reallywant exact decimal values, you should use BigDecimal
.
如果你真的想要精确的十进制值,你应该使用BigDecimal
.
回答by Niger
public static double roundNumber(double num, int dec) {
return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}
回答by Thomas Padron-McCarthy
Multiply by 100000. Add 0.5. Truncate to integer. Then divide by 100000.
乘以 100000。加 0.5。截断为整数。然后除以 100000。
Code:
代码:
double original = 17.77777777;
int factor = 100000;
int scaled_and_rounded = (int)(original * factor + 0.5);
double rounded = (double)scaled_and_rounded / factor;
回答by Arthur Ronald
Try the following
尝试以下
double value = Double.valueOf(String.format(Locale.US, "%1$.5f", 5.565858845));
System.out.println(value); // prints 5.56586
value = Double.valueOf(String.format(Locale.US, "%1$.5f", 5.56585258));
System.out.println(value); // prints 5.56585
Or if you want minimal amount of code
或者如果你想要最少的代码
Use import static
使用导入静态
import static java.lang.Double.valueOf;
import static java.util.Locale.US;
import static java.lang.String.format;
And
和
double value = valueOf(format(US, "%1$.5f", 5.56585258));
regards,
问候,
回答by Milhous
DecimalFormat roundFormatter = new DecimalFormat("########0.00000");
public Double round(Double d)
{
return Double.parseDouble(roundFormatter.format(d));
}
回答by wildriver
I stumbled upon here looking for a way to limit my double number to two decimal places, so not truncating nor rounding it. Math.Truncate gives you the integral part of the double number and discards everything after the decimal point, so 10.123456 becomes 10 after truncation. Math.Round rounds the number to the nearest integral value so 10.65 becomes 11 while 10.45 becomes 10. So both of these functions did not meet my needs (I wish that .Net had overloaded both of these to allow truncating or rounding up to a certain number of decimal places). The easiest way to do what I needed is:
我在这里偶然发现了一种方法来将我的双数限制为两位小数,因此不会截断或四舍五入。Math.Truncate 为您提供双数的整数部分并丢弃小数点后的所有内容,因此 10.123456 截断后变为 10。Math.Round 将数字四舍五入到最接近的整数值,因此 10.65 变为 11,而 10.45 变为 10。所以这两个函数都不能满足我的需要(我希望 .Net 已经重载了这两个函数以允许截断或四舍五入到某个小数位数)。做我需要的最简单的方法是:
//First create a random number
Random rand = new Random();
//Then make it a double by getting the NextDouble (this gives you a value
//between 0 and 1 so I add 10 to make it a number between 10 and 11
double chn = 10 + rand.NextDouble();
//Now convert this number to string fixed to two decimal places by using the
//Format "F2" in ToString
string strChannel = chn.ToString("F2");
//See the string in Output window
System.Diagnostics.Debug.WriteLine("Channel Added: " + strChannel);
//Now convert the string back to double so you have the double (chn)
//restricted to two decimal places
chn = double.Parse(strChannel);