I was reading about data types in C. I found the range of long double
is more than that of double
in java. For very huge number we can use long double in C. If I want to store the same number in java what we have to do? which datatype we can use?
double in java takes 8 bytes(64 bits) IEEE 754. it Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
longdouble in c takes 10 bytes (80 bits)
Can anyone tell me is there any replacement of longdouble in java
Double longValue() in Java with ExamplesThe longValue() method of Double class is a built in method to return the value specified by the calling object as long after type casting. Parameters : It takes no parameters. Return type : It returns an long value i.e. the type casted value of the DoubleObject.
Let's check a straightforward way to cast the double to long using the cast operator: Assert. assertEquals(9999, (long) 9999.999); Applying the (long) cast operator on a double value 9999.999 results in 9999.
In an article on MSDN, it states that the double data type has a range of "-1.79769313486232e308 .. 1.79769313486232e308". Whereas the long data type only has a range of "-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807".
Though not a replacement, you can use java.lang.math.BigDecimal.You can roughly store billion of digits until you run out of memory. Its an arbitrary precision class, it will get as large as you'd like until your computer runs out of memory.
As per the documentation of BigDecimal:
Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, which represents the number of digits to the right of the decimal point. The number represented by the BigDecimal is (unscaledValue/10scale). BigDecimal provides operations for basic arithmetic, scale manipulation, comparison, hashing, and format conversion.
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