Java , .
: 2.35 10000, .
Double a = 2.85d / 10000;
System.out.println("1. " + a.doubleValue());
a = 2.85d * 100000000;
System.out.println("2. " + a.doubleValue());
:
, , . , : 0.000285 285000000. , java.math.BigDecimal. BigDecimal.valueOf() Double BigDecimal .toPlainString(), .
import java.math.BigDecimal;
Double a = 2.85d / 10000;
System.out.println("1. " + BigDecimal.valueOf(a).toPlainString());
a = 2.85d * 100000000;
System.out.println("2. " + BigDecimal.valueOf(a).toPlainString());
:
The only drawback of the above method is that it generates long strings of numbers. You can limit the value and round the number to 5 or 6 decimal places. You can use a class for this java.text.DecimalFormat. In the following example, we round the number to 4 decimal places and print the result.
import java.text.DecimalFormat;
Double a = 2.85d / 10000;
DecimalFormat formatter = new DecimalFormat("0.0000");
System.out.println(formatter .format(a));
Result:
0.0003
I just tried to compress this code with one line, it will print the value "a" with two decimal places:
new DecimalFormat("0.00").format(BigDecimal.valueOf(a).toPlainString());
Happy conversion :)
source
share