SAVE UP to ₹4999/- OFF On All Salesforce Courses Claim Offer

3

Floating Point Data Type in APEX

A floating-point variable can represent an extensive range but with a fixed number of digits in accuracy. While Apex doesn’t have a specific data type named floating-point, it does offer the Double and Decimal data types to represent floating-point numbers.

A floating-point number, as a decimal number, can represent a wide range of values, including very large or small numbers. Unlike Integer or Long, which only store whole numbers, Double can represent numbers with decimal parts.

There are two essential data types that can be considered as Float Data Types in Apex.

Types Of Float Data Type in Apex

There are two essential data types that can be considered as Float Data Types in Apex.

1. Double (8 bytes/64 bits)

A 64-bit number that includes a decimal point, Double has a minimum value of -263 and a maximum value of 263-1. While Double offers a wide range, they can only represent some decimal numbers perfectly due to limitations in how computers store numbers. This can lead to slight rounding errors in some calculations. Scientific notation (e) for Doubles isn’t supported.

For example:

Double pi = 3.14159;
Double e = 2.7182818284D;

Syntax: Double db = 99.5;

2. Decimal

A Decimal is an arbitrary precision number that includes a decimal point. Currency fields are automatically assigned the decimal data type. If you require extremely high precision for decimal calculations, consider using the Decimal data type. It’s specifically designed for financial calculations. 

  • Suppose the number of decimal places is not explicitly stated. In that case, the scale of a decimal is determined by the item from which it is formed. A scale is a decimal place count. To set the scale of a decimal, use the setScale method.
  • The size of the Decimal is determined by the scale of the field that the query returns if it is created as part of a query.
  • The scale is the number of characters after the string’s decimal point if the decimal is formed from a string.
  • If a non-decimal number is used to make a decimal, it must first be transformed into a string.

For Example:

Decimal accountBalance = 500.00; // High-precision for financial calculations

Syntax: Decimal deci = 99.5;
salesforce-developer
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!