double a=816992306.6297043221;
System.out.println("a="+a);
The problem with this is that the number is too large for double, and it gives the following output:
a=8.169923062970433E7
When I decrease the number, the result gets displayed correctly. What is the efficient solution for this problem?
You can use a BigDecimal
number instead of double
:
Immutable, arbitrary-precision signed decimal numbers.
BigDecimal
can represent any number with the choosen precision .
Double
has limits on maximum and minimum values and on precision .
I think it is a precision problem.
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
You can check this link out for learning about format.
You can use DecimalFormat Class
double d1 = 3.14159;
double d2 = 1.235;
DecimalFormat format = new DecimalFormat("#.##");
double roundedD1 = format.format(d1); // 3.14
double roundedD2 = format.format(d2); // 1.24
If you want to set the precision during runtime.
format.setMaximumFractionDigits(precision);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.