问题
if
double d = 1.999e-4
I want my output to be 0.0001999.
How can I do it?
回答1:
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(d);
You can explore the sub classes of NumberFormat
class to know more details.
回答2:
You can do it like this:
double d = 1.999e-4;
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(7);
System.out.println(nf.format(d));
Check out the documentation of NumberFormat
's methods to format your double
as you see fit.
DecimalFormat
is a special case of NumberFormat
as its constructor states, I don't think that you need its functionality for your case. Check out their documentation if you are confused. Use the factory method getInstance()
of NumberFormat
for your convenience.
回答3:
I suppose there is a method in BigDecimal Class called toPlainString()
.
e.g. if the the BigDecimal is 1.23e-8 then the method returns 0.0000000124.
BigDecimal d = new BigDecimal("1.23E-8");
System.out.println(d.toPlainString());
Above code prints 0.0000000123, then you can process the string as per your requirement.
回答4:
If all you want is to print like that.
System.out.printf("%1$.10f", d);
you can change 10f, 10=number of decimal places you want.
回答5:
Take a look over
java.text.DecimalFormat
and
java.text.DecimalFormatSymbols
来源:https://stackoverflow.com/questions/13064567/java-beginner-converting-scientific-notation-to-decimal