Skip to Main Content
  • Questions
  • How to make a number formatted and right justified


Question and Answer

Connor McDonald

Thanks for the question, Mian.

Asked: January 14, 2016 - 6:56 pm UTC

Answered by: Connor McDonald - Last updated: January 15, 2016 - 12:53 pm UTC

Category: Developer - Version: Oracle

Viewed 10K+ times! This question is

You Asked

Hi Gurus,
I am working on a store procedure & I have a numeric field (MY_TABLE.NUM_FIELD) which is NUMERIC (18,2) & has results like below

My store procedure requirement is to provide max 9 numbers including decimal (".") & 2 numbers after decimal point. Also my requirement is to add values up to 2 decimal points if the num is a whole num by keeping num_field right justified.
So far I have tried RPAD (TO_CHAR (NUM_FIELD, '99999.00'), 9). This logic gives me required decimal fields but then it makes output fields left justified (probably because to_char makes a num field a string). The perfectly desired results should be like below


Thank you so much in advance for help & support.

and we said...

You didnt use the 'code' tags, so your results look same as your before data. So I'm taking a guess here.

Like this ?

SQL>   with data as
  2    (select 1.2 x from dual union all
  3     select 5     from dual union all
  4     select 22.7    from dual union all
  5     select 3.45  from dual )
  6  select
  7    x, to_char(x,'99999.99') x1
  8    from data;

         X X1
---------- ---------
       1.2      1.20
         5      5.00
      22.7     22.70
      3.45      3.45

and you rated our response

  (1 rating)

Is this answer out of date? If it is, please let us know via a Review


decimal separator

January 15, 2016 - 12:38 pm UTC

Reviewer: Rob van den Berg from The Netherlands

Hi Mian,

Some additional comment to Connor's anwer.
RPAD means padding (spaces, by default) to the right. Your requirement to keep the number right justified suggests to use LPAD.
Decimals separators can be a dot or a comma depending on nls settings. You can denote a decimal separator while converting a number to a character by using a 'D'.
The input might be between -1/2 and 1/2: I guess you want a leading zero in that case. That's why you would need to denote a '0' before the decimal separator in the conversion format string.
Finally, if you meant to output max nine characters, then you might need

LPAD (TO_CHAR (NUM_FIELD, '999990D00'), 9)

Connor McDonald


January 15, 2016 - 12:53 pm UTC

Nice additional input.