java rounding numbers [duplicate] - java

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 9 years ago.
I retrieve some data from a database but the number has a lot of digits and I want to round it. The thing is I do not want the whole number to be rounded.
Example :
I draw number 4.1010359882326385E10 which is of type long.
I want to round it to 4.10103598823264.
That is to round the number after the 15th digit.
Any way I can do this?

E10 in your number is a scientific notation, that says you number is actually 41010359882.326385. If you transform it to 4.10103598823264, you are not doing a rounding, you are doing something else.

Related

Java BigDecimal divide and get digit to N places [duplicate]

This question already has answers here:
BigDecimal setScale and round
(2 answers)
Set specific precision of a BigDecimal
(5 answers)
Closed 1 year ago.
I am trying to write a program that has a step in it where I would like to divide two numbers and get a decimal number to 60 places.
For instance, I would like to divide 1 by 17 and get 0.016393442622950819672131147540983606557377049180327868852459 without losing any precision.
I am trying to store the number in a BigDecimal but I am having trouble finding a good way to accomplish this without losing the precision after the 16th digit or so

Round answer based on what the user inputs? [duplicate]

This question already has answers here:
How to Java String.format with a variable precision?
(4 answers)
Java printf using variable field size?
(4 answers)
Closed 2 years ago.
I'm a new programmer, and I'm coding a basic decimal calculator. (in Java)
If the user inputs how many decimals they want to round to, say 2, how would I incorporate that into the code, and have the answer round to 2 decimals?
Right now, I have something like this:
System.out.printf("%.3f %n", ans);
But that will always round to 3 decimals, no matter what the user input is.
Here's a link to the whole code, if necessary.
Is there a basic way to this?

Format a float to 2 decimal places without changing variable type [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?
Here's a way to do it:
https://stackoverflow.com/a/153785/4119650
It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.

Remove Decimal from BigDecimal [duplicate]

This question already has answers here:
Java BigDecimal remove decimal and trailing numbers
(6 answers)
Closed 6 years ago.
I have a BigDecimal number, I just want to remove the decimal numbers from it,
for example if I have 200.88 then output should be 200?
I tried Bigdecimal rounding function but they wont do the job
You can specify the rounding mode to ROUND_FLOOR when you use round.

Java - 100 decimal places in variable [duplicate]

This question already has answers here:
How many significant digits do floats and doubles have in java?
(6 answers)
Closed 7 years ago.
I have a problem when it comes to calculating pi with more than 15 decimal places(i used double).
My result looks quite good except my variable is limited to 15 decimal places:
3.140592653839794
Anybody could tell me what i have to do if i want more decimal places?
Thanks and Greeting!
Use java.math.BigDecimal instead of double for arbitrary (finite) precision.

Categories

Resources