In Java, you should use double instead of decimal when you need to store a floating-point number with more precision than
decimal can offer.
double is a 64-bit floating-point number, while decimal is a 16-bit floating-point number. This means that
double can store numbers with more precision than decimal.
double is also more efficient than decimal for storing large numbers.
Here is a table summarizing the differences between double and
decimal in Java:
Feature
double
decimal
Precision
64 bits
16 bits
Range
±1.7976931348623157e+308
±3.4028234663852886e+38
Efficiency
More efficient for storing large numbers
Less efficient for storing large numbers
In general, you should use double when you need to store a floating-point number with more precision than
decimal can offer. You should use decimal when you need to store a floating-point number with a fixed precision, such as currency.
Here are some examples of when you should use double instead of
decimal:
Storing the value of pi
Storing the weight of an object in grams
Storing the temperature in Celsius
Storing the area of a circle in square meters
Here are some examples of when you should use decimal:
Storing the price of an item in dollars
Storing the balance of an account in cents
Storing the interest rate on a loan in percent
Storing the exchange rate between two currencies
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Java, you should use
doubleinstead ofdecimalwhen you need to store a floating-point number with more precision thandecimalcan offer.doubleis a 64-bit floating-point number, whiledecimalis a 16-bit floating-point number. This means thatdoublecan store numbers with more precision thandecimal.doubleis also more efficient thandecimalfor storing large numbers.Here is a table summarizing the differences between
doubleanddecimalin Java:In general, you should use
doublewhen you need to store a floating-point number with more precision thandecimalcan offer. You should usedecimalwhen you need to store a floating-point number with a fixed precision, such as currency.Here are some examples of when you should use
doubleinstead ofdecimal:Here are some examples of when you should use
decimal: