简体   繁体   English

在Java ME中将double加倍到5位小数

[英]Rounding a double to 5 decimal places in Java ME

如何在不使用DecimalFormat情况下将double加倍到5位小数?

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 : 假设round的值是一个名为xdouble精度值:

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位小数, 1e6 factor设为1e6 ,依此类推。

Whatever you do, if you end up with a double value it's unlikely to be exactly 5 decimal places. 无论你做什么,如果你最终得到一个double值,它不可能正好是 5位小数。 That just isn't the way binary floating point arithmetic works. 这不是二进制浮点运算的工作方式。 The best you'll do is "the double value closest to the original value rounded to 5 decimal places". 你要做的最好的是“ 最接近原始值的双精度值四舍五入到小数点后5位”。 If you were to print out the exact value of that double, it would still probably have more than 5 decimal places. 如果要打印出该double的确切值,它仍可能有超过5个小数位。

If you really want exact decimal values, you should use BigDecimal . 如果您确实需要精确的十进制值,则应使用BigDecimal

Multiply by 100000. Add 0.5. 乘以100000.加0.5。 Truncate to integer. 截断为整数。 Then divide by 100000. 然后除以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;

如果您对外部库没有问题,可以查看一下microfloat ,特别是MicroDouble.toString(double d,int length)

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

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, 问候,

DecimalFormat roundFormatter = new DecimalFormat("########0.00000");

public Double round(Double d)
    {
        return Double.parseDouble(roundFormatter.format(d));
    }
public static double roundNumber(double num, int dec) {
        return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}

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.Truncate为您提供双数的整数部分,并丢弃小数点后的所有内容,因此截断后10.123456变为10。 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). Math.Round将数字四舍五入到最接近的整数值,因此10.65变为11而10.45变为10.所以这两个函数都不符合我的需要(我希望.Net已经超载了这两个以允许截断或舍入到某个小数位数)。 The easiest way to do what I needed is: 做我需要的最简单的方法是:

//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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM