Round a double value to 10 digits in Java? [duplicate] - java

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I was looking for a simple way to round a double value to 10 digits but I didn't found even one way that look good to me, everything was too complicated.
I hope someone could help me, for example: the value 0.83426945721236485 will become 0.8342694572
Thank you in advance.

Simple.
DecimalFormat df = new DecimalFormat("0.0000000000");
System.out.println(df.format(0.83426945721236485));
Take a look at the documentation:
https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html

Decimal places are not meaningful internally for double, because it is a binary floating point.
You can, of course, choose to display it in any format supported by DecimalFormat, as suggested in a prior answer.
If you want a ten decimal place internal representation, you should be using BigDecimal with scale factor 10.

Related

How i round off two decimal point in java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed last year.
Like it comes : 692589
i want the value similar : 69.23
I want to know more than one process to perform this task in java
you can use DecimalFormat("0.00") to ensure the number is round to 2 decimal places. and maybe this can help you.
https://mkyong.com/java/java-display-double-in-2-decimal-points/#:~:text=format(%E2%80%9C%25.,double%20to%202%20decimal%20places.

How to round a float number in Java if its a whole? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
How do I round a float number if it returns a whole value? And, how do I round if it's like:
5/2 = 2.5
and NOT like this:
5/2 = 2.50000000
A double does not allow you to specify the number of decimal places it has; you may be able to set as many as you wish to 0, but they are still there. (Note that, mathematically, the two values you show for 5/2 are the same.) What you can do is control how many get displayed; since you haven't specified how you are attempting to display this value, I can't help in how to modify it to limit the number of decimal places to show.
Whenever needed, you can make some modifications on your double, such as using the setRoundingMode method of DecimalFormat class, along with the RoundingMode enum.
As mentioned before though, 2.5 is the same with 2.500000 in a double, you do not have to change its digits.
I guess you are trying to print your double and you get a number of unwanted digits in the output. In that case, I suggest though that you convert your double into a formatted string and use it as such:
System.out.println(String.format("%.2f", myDouble));
You are a bit confused I think...
2.5 and 2.50000000 are the exact same thing!
They are just written differently.
I don't really understand what you are trying to achieve but you can round floats like this:
float result = Math.round(someFloat);
If you always want to round up or down regardless of what the number is use:
float result = (float)Math.ceil(someFloat);
or
float result = (float)Math.floor(someFloat);

Rounding to a fixed relative precision [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Round a double to 3 significant figures
(7 answers)
Closed 12 months ago.
I get number representing strings like
248.03500000066338
313.44999999979470
4.2346999999
and I need to round them to something like
248.035
313.45
4.2347
while keeping a fixed relative precision of let's say 6 significant figures. I could do it using Math.log10, computing what absolute precision is needed, and rounding correspondingly, but I wonder if there's a simple way.
Ideally, the resulting number should be such that it does not produce the trailing nines when converted to string, but this is not needed and maybe impossible.
It might not be the most performant solution but I think this is the easiest one:
BigDecimal input = new BigDecimal("248.03500000066338");
double rounded = input.round(new MathContext(6)).doubleValue();

Using Math.round to round to the nearest tenth? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 9 years ago.
I just wanted to ask a quick question regarding the Math.round method. I'm trying to compute the division of two ints into a double. The equation looks like this: 199/39. When I do this it returns 5.0 as the answer. I know the answer should be 5.1 with some more decimals. I have been told to use the Math.round method to round it to the nearest tenth, but I have no idea how to accomplish this. Should I change that double variable to a int and make it int/int=int? I'm not sure how Math.round even works to get 5.1 as I've read it only rounds to the nearest integer not decimal point. Any help would be fantastic.
P.S This is homework, but I ask only because I can't find any information in my notes, slides, or book on how to use Math.round.
You don't need Math.round() to get a resultant decimal value. If you divide an int by an int, you will get an int. if you want a decimal, then cast double to one of the input values. Then you will get a double as a result.
(double) 199 / 39
199.0 / 39
// both return
5.102564102564102
I know the answer should be 5.1 with some more decimals.
Not with integer division it shouldn't. 5 is correct.
If you want a floating-point answer, you need to provide at least one floating-point operand, e.g. 199/39.0.
You can then format that for printing with as many or few decimal places you like, with System.printf() or DecimalFormat.
You can't round the floating-point value itself to decimal places, because it doesn't have decimal places, it has binary places.
See this question for a full discussion, especially my answer there.
Your specific answer:
roundedNumber = (double)Math.round(unRoundedNumber * 10) / 10;
In general, the equation is:
roundedNumber =
(double)Math.round(unRoundedNumber * Math.pow(10, digitsToRoundTo))
/ Math.pow(10, digitsToRoundTo);
Thanks for all the help. After reading your posts and looking at it some more I ended up doing ratio=((double)199/39) and then going
ratio=(double)Math.round(ratio*10)/10. Doing that got me the 5.1 i was looking for.

Floating point multiplication in java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to round a number to n decimal places in Java
When multiplying two numbers in java happens this:
double a = 9.495 * 100;
Expected result:
a = 949.5;
But the obtained result is:
a = 949.4999999999999
When I try to round number 9.495 in two decimal places the result is 9.49 instead of 9.50
Any ideas how to solve this problem?
If you want accurate floating point computations, do not use the float or double types, but rather make use of the BigDecimal class.
This is a side effect of floating point calculations, and is well understood, but not necessarily intuitive. This question has actually been asked literally thousands of times, and you need to study how floating point arithmetic works.
To get around this, if you only need 2-decimal precision, then use a integer instead.
For example, if you're dealing with currency, and you want to buy a 100 items for $4.95, then you represent the cost of that value as the integer "495", an multiply that by 100, which gives you "49500". You always treat the last two digits as cents, so "49500" is $495.00.
You cannot. Floating point and double precision numbers in a computer cannot represent all possible values.

Categories