Java supports constants of character, string, boolean, and numeric values.
import java.lang.Math;
import java.math.BigDecimal;
class Main {
public static void main(String[] args) {
// 'final' declares a constant value.
final String s = "constant";
System.out.println(s);
// A `final` statement can appear before Data Type.
final Integer n = 500000000;
// A numeric constant has type usually declared by user.
final Double d = 3e+20 / n;
System.out.println(d);
// To convert a numeric constant type
// It is required to cast to `BigDecimal` first
// Then cast to other data types (like `BigInteger`)
final BigDecimal e = new BigDecimal(d);
System.out.println(e.toBigInteger());
System.out.println(Math.sin(d));
}
}
javac Constants.java
java Constants
# constant
# 6e+11
# 600000000000
# -0.28470407323754404
Reference:
BigDecimal Class in Java - GeeksforGeeks
is there anyway to convert from Double to BigInteger? – Java (tutorialink.com)
Top comments (0)