I have a problem which I have to print with long decimal places. such as 1/3 = 0.33333333333333333333333333333333333333 (very very long) when using C, we can use printf("%.30f", a); but I don't know what to do using Java
You won't get that many decimal places of precision in IEEE754 binary floating point numbers in either C or Java. Your best bet would be to use BigDecimal and perform the arithmetic with a particular precision. For example:
import java.math.*;
public class Test {
public static void main(String[] args) throws Exception {
MathContext context = new MathContext(30);
BigDecimal result = BigDecimal.ONE.divide(new BigDecimal(3), context);
System.out.println(result.toPlainString());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With