Is there any way to convert scientific notation to fixed notation in Java.
Currently I have got the following code:
DecimalFormat dfmt=new DecimalFormat("######:#######");
print(dfmt.format((new BigDecimal((Math.pow(10,-3)*3000),MathContext.Decimal64)).stripTrailingZero()));
The above code does converts scientific notation to fixed notation,however if the result is greater than zero the decimal place is ignored.
In the above example it prints 3; instead of 3.0
Use the the pattern 0.0:
BigDecimal d = new BigDecimal("12345678901234567890"); // large integer
System.out.println(new DecimalFormat("0.0").format(d));
Outputs (in my locale):
12345678901234567890.0
Related
I have a number with value
0.947
Now, I use DecimalFormat API of java.text with the following pattern and RoundingMode-
double numberToFormat = 0.947;
DecimalFormat restrictTo1DecimalPlace = new DecimalFormat("0.0%");
restrictTo1DecimalPlace.setRoundingMode(RoundingMode.DOWN);
String formattedString = restrictTo2DecimalPlace.format(numberToFormat);
Now, I was expecting the value of formattedString to be 94.7% but its 94.6%.
I know the value has been set to RoundMode.Down but then why does value of following are not rounded down -
0.9471 -> 94.7%
0.9447 -> 94.4%
The closest floating point number to 0.947 is actually
0.94699999999999995292654375589336268603801727294921875
This is what your computer stores as a double when you write 0.947.
Rounding that down gives you 94.6%.
That's life I'm afraid. If you want exact decimal behaviour then use a decimal type! See data type to represent a big decimal in java
I'm trying to prevent a java double from printing in scientific notation. I know I can use DecimalFormat for this, but this produces a String. How can I produce an actual double?
I've tried:
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(8);
double n = Double.parseDouble(df.format(z));
However, this still produces a double in scientific notation.
A format is not a property of a double, the data type. This defines just a set of values.
Printing always requires a conversion to a String, so DecimalFormat is fine.
As for using an entirely different set of decimal fractions as values, see java.math.BigDecimal. This is the set of "all" decimal fractions, with a dynamic number of fractional digits, operations and control of round-off.
I would like to print a BigDecimal in scientific notation so that the there are, say, 5 digits after the decimal point. For example: -3.12345E-51, 9.12345E100
.setScale() does not work in this case, because I don't know in advance the exponent of number in scientific notation. Moreover, BigDecimal doesn't seem to have a getExponent() method.
What's the best way to achieve this? Thank you in advance.
You should use DecimalFormat to print numbers in specified format:
BigDecimal bigDecimal = new BigDecimal(-3.12345E-51);
DecimalFormat format = new DecimalFormat("0.#####E0");
System.out.println(format.format(bigDecimal));
Also you can configure count of fraction digits using method DecimalFormat#setMaximumFractionDigits(int):
DecimalFormat format = new DecimalFormat("0E0");
format.setMaximumFractionDigits(6);
If I want a double to have 9 decimal places, do I have to convert it to a string and then back to a double to do this (string methods are the only methods I'm seeing for setting the precision)?. In any case, what is the conventional way for setting the precision for a double if, for example, I want my method to return a double with 9 decimal places.
You're getting confused between a double's internal representation and it's display. Internally double numbers are always stored the same way but you can of course use a formater like DecimalFormat to return 9 decimal points from your double number.
Use Big Decimal to get 9 decimal places
or
double d = 1.2345672626346;
DecimalFormat df = new DecimalFormat("#.#########");
System.out.print(df.format(d));
You can't control precision with primitives in Java. You need to use BigDecimal. Read this excellent tutorial..
You could also use:
DecimalFormat df = DecimalFormat.getInstance(Locale.getDefault());
df.setMinimumFractionDigits(9); //sets 9 digits after the '.'
String formatedString = String.format("%.2f");
it set the presition to 5 digit o any.
if you only want to print it use this way.
System.out.printf("%.5f",123.2342622467);
I have a BigDecimal object, myNumber, with unknown length. For example: 12345678.
I always want to divide this number by 1 million, so I do:
myNumber.divide(BigDecimal.valueOf(1000000))
I get 12.345678.
I want to display this as a string "12.345678", without cutting off ANY decimal places.
So I do
myNumber.divide(BigDecimal.valueOf(1000000)).toString()
This works fine with the above example. But if myNumber is something ridiculously small or big, such as:
0.00000001
After dividing 0.00000001 by a million and converting to string, it displays as scientific notation, which is not what I want. I want it to always display in full decimal format (in this case, 0.00000000000001).
Any ideas?
You have to perform the division using the variant of divide() that includes a rounding mode and a scale, and set the scale large enough to include all the fractional digits.
int s = myNumber.scale();
BigDecimal result = myNumber.divide(BigDecimal.valueOf(1000000), s+6, RoundingMode.UNNECESSARY);
Then use toPlainString() to format.
I think that BigDecimal.toPlainString() is the method you need. However, note that the division itself will throw an exception when the decimal representation is infinite, such as with 1/3.
BigDecimal.toString or toPlainString would help.
You can use BigDecimal.toPlainString() to return "a string representation of this BigDecimal without an exponent field".
The scientific notation on the other hand is returned by BigDecimal.toEngineeringString().