Rounding a double to 5 decimal places in Java ME

人走茶凉 提交于 2020-02-10 09:02:51

问题


How do I round a double to 5 decimal places, without using DecimalFormat?


回答1:


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 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.




回答2:


Whatever you do, if you end up with a double value it's unlikely to be exactly 5 decimal places. 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". If you were to print out the exact value of that double, it would still probably have more than 5 decimal places.

If you really want exact decimal values, you should use BigDecimal.




回答3:


Multiply by 100000. Add 0.5. Truncate to integer. Then divide by 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;



回答4:


If you are okay with external libraries, you can have a look at microfloat, specifically MicroDouble.toString(double d, int length).




回答5:


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,




回答6:


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

public Double round(Double d)
    {
        return Double.parseDouble(roundFormatter.format(d));
    }



回答7:


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



回答8:


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:

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


来源:https://stackoverflow.com/questions/1451149/rounding-a-double-to-5-decimal-places-in-java-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!