When using the TO_CHAR()
function to format a number in Oracle Database, you can use the V
format element to a return a value multiplied by 10n (and if necessary, round it up), where n
is the number of 9
s after the V
.
Examples
Here’s an example to demonstrate:
SELECT TO_CHAR(1, '9V9') FROM DUAL;
Result:
10
Here are some more examples:
SELECT
TO_CHAR(1, '9V99') AS "99",
TO_CHAR(1, '9V999') AS "999",
TO_CHAR(1, '9V9999') AS "9999",
TO_CHAR(1, '9V99999') AS "99999",
TO_CHAR(74, '99V999999') AS "999999"
FROM DUAL;
Result:
99 999 9999 99999 999999 _______ ________ _________ __________ ____________ 100 1000 10000 100000 74000000
Here are some examples that use fractions:
SELECT
TO_CHAR(1.23, '9V99') AS "r1",
TO_CHAR(0.23, '9V99') AS "r2",
TO_CHAR(-0.23, '9V99') AS "r3",
TO_CHAR(74.8934, '99V999999') AS "r4"
FROM DUAL;
Result:
r1 r2 r3 r4 _______ _______ _______ ____________ 123 23 -23 74893400
We can use the fm
modifier to suppress any padding, such as leading/trailing spaces:
SELECT
TO_CHAR(1.23, 'fm9V99') AS "r1",
TO_CHAR(0.23, 'fm9V99') AS "r2",
TO_CHAR(-0.23, 'fm9V99') AS "r3",
TO_CHAR(74.8934, 'fm99V999999') AS "r4"
FROM DUAL;
Result:
r1 r2 r3 r4 ______ _____ ______ ___________ 123 23 -23 74893400
Rounding
Rounding occurs if necessary:
SELECT
TO_CHAR(1.1152, '9V99')
FROM DUAL;
Result:
112