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).
Related
This question already has answers here:
The accuracy of a double in general programming and Java
(2 answers)
Closed 2 years ago.
I am trying rounding and format techniques in Java as I have to keep my values in database with some specific formats.
My database column has the data precision of 18,4 which means 14 integers and 4 decimal places at max.
Now I am trying max possible value case which is 99999999999999.9999. When I execute the below code, I am getting a rounded value of 1000000000000000. However, I want to store the exact value provided.
Can anyone suggest to keep it as it is in java variable?
DecimalFormat df2 = new DecimalFormat( "#.####" );
df2.setRoundingMode(RoundingMode.CEILING);
double number = Double.parseDouble("99999999999999.9999");
System.out.println("Format: " + df2.format(number));
You can use BigDecimal
BigDecimal number = new BigDecimal("99999999999999.9999");
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:
Format double value in scientific notation
(4 answers)
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I have been working with big numbers (with more that 20 digits) and for that reason I use Double . The form I get the numbers is like this 8.653762536765E28.
What I want to do is just display the first 2 decimal digits. I want it like 8.65E28.
I tried to find about formatting double values but I wan't able to do it. The result I was getting was 86537...12312.00 .
What do you thing is a good approach for this case? How can I manage only the digits in front of the E (the base) and not the whole number?
I think this is what you want to achieve:
double d = 8.653762536765E28;
DecimalFormat df = new DecimalFormat("0.00E0");
System.out.print(df.format(d));
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 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();