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();
Related
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.
This question already has answers here:
How to format a number to Fixed length, space padded, thousand separator, 2 decimals in Java
(3 answers)
Closed 6 years ago.
If I want to format output in a GUI (specifically having only 2 decimal places for a number), would I use the DecimalFormat class along with the printf() method, or is there another way of doing this?
Basically you will want to round the number leaving 2 decimal places, or, in other words, round only the digits after the second digit after a decimal point, so:
number = Math.round(number * 100)/100;
Depending on your exact needs use Math.floor() or Math.ceil()
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.
This question already has answers here:
How to print formatted BigDecimal values?
(7 answers)
Closed 9 years ago.
I have a BigDecimal whose value I'd like to convert to a string and NOT lose any precision.
The format I'd like to use is ###.###,## (comma for thousands, period for decimals).
The only way I've made this work is using
DecimalFormat formatter = new DecimalFormat("###.###,##");
formatter.format(bd.doubleValue());
... but I'm afraid I might lose information this way, and precision is a must because I'm dealing with currency (every penny counts).
Additional information: I will only be dealing with sums of up to 1 million if that is of any help.
To be on the safe side, you could multiple your BigDecimal by 100.
Then get its intValue -> say N. Then get N/100 and N%100.
This way you cannot lose precision (N <= 100 million cannot overflow int).
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.