PLSQL | TRUNC Function
The TRUNC function is an inbuilt function in PLSQL which is used to return a number truncated to a particular number of decimal places.
Syntax:
TRUNC( number, decimal_places )
Parameters Used:
This function accepts two parameters which are illustrated below:-
- number – This is the input number which is going to be truncated to a certain number.
- decimal_places – This is also a input number which specifies that up to what number after decimal point should be the output of this function.
Return Value:
This function returns a numeric value truncated to a particular number of decimal places.
Supported Versions of Oracle/PLSQL:
- Oracle 12c
- Oracle 11g
- Oracle 10g
- Oracle 9i
- Oracle 8i
Let’s see some examples which illustrate the TRUNC function:
Example-1:
DECLARE Test_Number number := 5.5; BEGIN dbms_output.put_line(TRUNC(Test_Number number)); END;
Output:
5
In the above example, the truncated value of 5.5 is 5
Example-2:
DECLARE Test_Number number1 := 5; Test_Number number2 := 0; BEGIN dbms_output.put_line(TRUNC(Test_Number number1, Test_Number number2)); END;
Output:
5
In the above example, the truncated value of (5, 0) is 5 because 5 is not having any decimal point and hence it returns 5 as the output.
Example-3:
DECLARE Test_Number number1 := 15.3123; Test_Number number2 := 2; BEGIN dbms_output.put_line(TRUNC(Test_Number number1, Test_Number number2)); END;
Output:
15.31
In the above example, the truncated value of 15.3123 is 15.31 because here 2 is at the place of decimal_place parameter and it shows that the output value must contain 2 decimal number after the decimal point.
Advantage:
This function is used to return a number truncated to a particular number of decimal places.
Please Login to comment...