Open In App

Difference Between Decimal and Float in PL/SQL

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Have you ever thought about the differences between Decimal and Float in PL/SQL? These two numeric data types play important roles in database programming that offer unique features for handling numeric values.

In this article, we’ll explore What are Decimals and Float along with their syntax, usage, and performance implications. Whether we’re new to programming or a seasoned developer, understanding these differences will help us make better-informed decisions in our PL/SQL projects.

What is Decimal?

Decimal which is also known as Numeric and fixed-point numeric data type is used for storing numeric values with a predetermined precision and scale. Decimal is used in applications where exact numeric values are required, such as in financial calculations, where accuracy is important.

Syntax:

DECIMAL(precision, scale)

Explanation:

  • Precision: Total number of digits.
  • Scale: Number of digits after the decimal point.

Example 1:

SET @decimal_num = 123.45;
SELECT CONCAT('Decimal Number: ', @decimal_num) AS output;

Ouput:

dec

Output

Explanation: In the above query we sets a session variable @decimal_num to 123.45 and then concatenates it with a string to display the output as 'Decimal Number: 123.45'.

What is Float?

FLOAT is a floatingpoint numeric data type that allows for the flexible handling of approximate numeric values. It is suitable for scientific computations and scenarios where precise decimal representations are not required.

Syntax:

FLOAT(precision)

Explanation: Precision is the Total number of digits.

Example 1:

SET @float_num = 123.45;
SELECT CONCAT('Float Number: ', @float_num) AS output;

Output:

flex1

Output

Explanation: In the above query we will set the session variable @float_num to the value 123.45 and then select it with a concatenated string to display the output.

Difference Between Float and Decimal DataType

let’s compare the DECIMAL and FLOAT data types in SQL using a table:

Aspect

Decimal

Float

Precision

Exact

Approximate

Storage

Fixed

Variable

Usage

Financial calculations

Scientific computations

Performance

Slower

Faster

Example

DECIMAL(10, 2)

FLOAT(10)

Description

Used for exact numeric values

Used for approximate numeric values

Conclusion

Overall, Decimal and Float data types deal with different requirements in PL/SQL programming languages. Decimal is adapted to needs of applications that involve precision of numeric values, for example financial calculations, whose accuracy is of very importance. on the other hand, Float is chosen for scientific calculations in which variations of approximate solutions are possible and the flexibility regarding different scales is required. After reading this article you can easily implemented these datatypes in your required applications.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads