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

Breadcrumb

more

Connor and Chris don't just spend all day on AskTOM. You can also catch regular content via Connor's blog and Chris's blog. Or if video is more your thing, check out Connor's latest video and Chris's latest video from their Youtube channels. And of course, keep up to date with AskTOM via the official twitter account.

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 4.0.1.14

Viewed 1000+ times

Whilst you are here, check out some content from the AskTom team: Quick tip-identity columns

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

NUM_FIELD
-----------
186.2
34.89
76.4
25
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

NUM_FIELD
-----------
186.20
34.89
76.40
25.00

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

Reviews

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)


Rob
Connor McDonald

Followup  

January 15, 2016 - 12:53 pm UTC

Nice additional input.