Sub topic 4
Formatting numbers with TO_CHAR
This chapter covers TO_CHAR with a NUMBER as its first argument. It assumes you know basic SELECT syntax, the NUMBER and VARCHAR2 types, and the DUAL table. It also assumes you have seen format models in the previous chapters, including how the FM modifier works for dates.
The shape of the call
TO_CHAR(n [, 'format_model' [, 'nlsparam']])n is a NUMBER (or something Oracle converts to one). The result is a VARCHAR2 whose layout is set by the format model. If you leave out the model, Oracle uses the shortest text that represents the number. That text has no grouping, and a value between 0 and 1 loses its leading zero: TO_CHAR(0.5) returns .5. Reports and exports usually need a fixed layout, so you normally pass a model.
The diagram shows what the function does with a value and a model.
Digit placeholders: 9 and 0
Two elements hold digits. A 9 prints a digit if one exists at that position and a blank otherwise. A 0 always prints a digit, so it forces leading or trailing zeros.
SELECT TO_CHAR(42, 'FM9999') AS nines,
TO_CHAR(42, 'FM0000') AS zeros,
TO_CHAR(0.5, 'FM990.00') AS leading_zero,
TO_CHAR(0.5, 'FM99.00') AS no_leading_zero
FROM dual;The results are 42, 0042, 0.50 and .50. Use 0 in front of the decimal point if you want a leading zero for values below 1. Use 0 after the decimal point to keep trailing zeros, such as .50 instead of .5.
If a model ends in 9 after the decimal point and the value is whole, FM leaves the point behind. TO_CHAR(5, 'FM9999.99') returns 5.. Write FM9999.00 when you want 5.00, or FM9999 when you want 5.
Separators
| Element | Meaning | Example model | Output for 1234567.891 |
|---|---|---|---|
| `,` | Group separator, literal comma | `FM9,999,999.00` | `1,234,567.89` |
| `.` | Decimal point, literal period | `FM9,999,999.00` | `1,234,567.89` |
| `G` | Group separator from NLS settings | `FM9G999G999D00` | depends on the NLS settings |
| `D` | Decimal separator from NLS settings | `FM9G999G999D00` | depends on the NLS settings |
, and . always produce a comma and a period. G and D produce whatever the session or the nlsparam argument says. Use G and D when output must follow a locale. Use , and . when the output format is fixed, for example a file for another system. A model cannot start with a comma, and a comma cannot follow the decimal point.
Signs and negative numbers
By default Oracle reserves one character position at the left for the sign. A positive number gets a blank there and a negative number gets a minus. That is the leading blank you see without FM. These elements change the placement:
| Element | Effect | `TO_CHAR(-1234.5, ...)` |
|---|---|---|
| none | Minus on the left, blank for positive | `-1,234.50` with `FM9,999.00` |
| `MI` | Minus on the right | `1,234.50-` with `FM9,999.00MI` |
| `PR` | Angle brackets around negatives | `<1,234.50>` with `FM9,999.00PR` |
| `S` | Always show `+` or `-`, at the start (or the end if placed last) | `-1,234.50` with `FMS9,999.00` |
SELECT TO_CHAR(-1234.5, 'FM9,999.00MI') AS minus_right,
TO_CHAR(-1234.5, 'FM9,999.00PR') AS brackets,
TO_CHAR( 1234.5, 'FMS9,999.00') AS plus_sign
FROM dual;This returns 1,234.50-, <1,234.50> and +1,234.50. MI, PR and a trailing S must be the last element in the model.
Currency symbols
$prints a literal dollar sign.Lprints the local currency symbol fromNLS_CURRENCY.Cprints the ISO currency code fromNLS_ISO_CURRENCY.Uprints the dual currency symbol fromNLS_DUAL_CURRENCY.
$ is fixed text in your model. L, C and U depend on the session unless you override them with nlsparam.
SELECT TO_CHAR(1234.5, 'FM$9,999.00') AS dollars
FROM dual;This returns $1,234.50.
Rounding and overflow
TO_CHAR rounds to the number of decimal places in the model. It rounds half away from zero. It does not truncate.
SELECT TO_CHAR(2.5, 'FM9') AS r1,
TO_CHAR(-2.5, 'FM9') AS r2,
TO_CHAR(0.125, 'FM0.00') AS r3,
TO_CHAR(12345, '9999') AS overflow
FROM dual;The first three columns return 3, -3 and 0.13. The last returns a string of # characters, because 12345 has five integer digits and the model has room for four.
If you use a model only to round, use ROUND or TRUNC on the number first and format afterward. That keeps the arithmetic rule visible in the query instead of hidden in a format string.
Controlling padding with FM
Without FM, each 9 position that has no digit becomes a blank, and the result is as wide as the model plus the sign position. Compare the lengths:
SELECT LENGTH(TO_CHAR(42, '9999')) AS padded_len,
LENGTH(TO_CHAR(42, 'FM9999')) AS trimmed_len
FROM dual;The result is 5 and 2. The padded form lines up in fixed-width reports. The FM form is what you want in JSON, CSV, labels, or any string you will compare or concatenate. You can also wrap the unpadded call in TRIM, but FM is clearer and also removes trailing zeros after the point for 9 positions.
FM affects only the elements that follow it. It is a toggle, so putting a second FM later in the model turns padding back on. Normally you write it once at the start.
Language and separators with nlsparam
The third argument sets NLS values for this one call. List them in a single string, separated by spaces.
SELECT TO_CHAR(10000, 'FML99G999D99',
'NLS_NUMERIC_CHARACTERS = '',.'' NLS_CURRENCY = ''AusDollars''') AS aud
FROM dual;The result is AusDollars10.000,00. The decimal separator is a comma and the group separator is a period, because NLS_NUMERIC_CHARACTERS lists the decimal character first and the group character second. Inside a string literal, each quote in the value is doubled.
Other elements worth knowing
EEEEgives scientific notation:TO_CHAR(1234567.891, 'FM9.99EEEE')returns1.23E+06.RNgives Roman numerals for 1 to 3999:TO_CHAR(2026, 'FMRN')returnsMMXXVI. Without FM the result is padded to 15 characters.XXgives hexadecimal for non-negative integers:TO_CHAR(255, 'FMXX')returnsFF. Oracle rounds non-integers to an integer first, and negative values raise an error.Bshows blanks instead of zeros for a zero value, andVmultiplies by ten to the power of the digit count after it. They are rarely needed.
Common mistakes
- Forgetting FM. The leading blank breaks string comparisons and concatenation:
'Total: ' || TO_CHAR(5, '999')givesTotal: 5. - A model that is too narrow. You get
#characters, not an error. - Using
9where0is needed.FM99.00gives.50, which some parsers reject. UseFM90.00. - Hard-coding
,and.for locale-aware output. UseGandDwithnlsparam. - Placing
MI,PRorSin the wrong spot.MIandPRmust be last. A trailingSgoes at the end, and a leadingSgoes at the start. - Assuming truncation.
TO_CHAR(0.999, 'FM0.00')returns1.00, not0.99.
Exercise
Write a query that formats the value 98765.4321 as a currency string with a dollar sign, grouping commas, exactly two decimals and no leading blank. What does it return, and what would change if you removed FM?
Show answer
SELECT TO_CHAR(98765.4321, 'FM$99,999.00') FROM dual; returns $98,765.43. The value is rounded to two decimals. Without FM, the result gets a leading blank for the sign position, so it becomes ' $98,765.43'. Because the model has five integer digits, the padding also depends on the value: a smaller number such as 5 would show extra blanks between the sign position and the $.
Exercise
What does TO_CHAR(1234.5, 'FM999.00') return, and how do you fix it?
Show answer
It returns ####, a string of # characters, because 1234 has four integer digits and the model allows only three. Oracle does not raise an error. Widen the model to FM9,999.00 (or FM9999.00) to get 1,234.50 (or 1234.50).