Floating Point Data Type in APEX
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
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;
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.