Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - Format number with fullstop for thousand and comma for decimals

I've a number like this 3225,69 and I want to convert the display to:

3.225,69

Simply, I need full stop to separate thousand and comma to separate decimals:

3,69 -> 3,69 
1000 -> 1.000 
1500,20 -> 1.500,20
1 -> 1 (not 1,000)

Which mask should I use in to_char?

I'm my DB columns the comma means decimals.

like image 689
Mistre83 Avatar asked Jan 26 '15 15:01

Mistre83


People also ask

How do you use a comma and a dot in numbers?

Using Commas in Numbers (US, UK, and China) In the US, UK, and China, a comma is placed every 3 decimal places for numbers larger than 999. The decimal point is shown with a period (full stop). For example: 1,234.

What is the data type for decimal in Oracle?

{ DECIMAL | DEC } [(precision [, scale ])] The precision must be between 1 and 31. The scale must be less than or equal to the precision. If the scale is not specified, the default scale is 0.

Can comma be used as decimal point?

Both a comma and a period (or full-stop) are generally accepted decimal separators for international use. Three ways to group the number ten thousand with digit group separators. 1) Space, the internationally recommended thousands separator.


1 Answers

You can use the FM format modifier to have trailing decimal zeros blanked out:

select to_char(1, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.''') from dual;

TO_CHAR(1,'FM9G999G999D999','NLS_NUMERIC_CHARACTERS='',.''')
------------------------------------------------------------
1,      

But as you can see that leaves the decimal character behind; you can trim that off though:

with t as (
 select 3.69 as n from dual
 union all select 1000 from dual
 union all select 150.20 from dual
 union all select 1 from dual
 union all select 0.16 from dual
)
select n,
  to_char(n, '9G999G999D000') original,
  to_char(n, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.''') new,
  rtrim(to_char(n, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.'''),
    ',') as trimmed
from t;

         N ORIGINAL       NEW            TRIMMED       
---------- -------------- -------------- --------------
      3.69          3.690 3,69           3,69           
      1000      1,000.000 1.000,         1.000          
     150.2        150.200 150,2          150,2          
         1          1.000 1,             1            
       .16           .160 ,16            ,16            

I'm using the optional third NLS argument to the to_char() function to set the G and D characters independently from my session settings.

If you want to preserve the zero befor the decimal separator, just make the last 9 before the D into a 0:

with t as (
 select 3.69 as n from dual
 union all select 1000 from dual
 union all select 150.20 from dual
 union all select 1 from dual
 union all select 0.16 from dual
)
select n,
  to_char(n, '9G99G990D000') original,
  to_char(n, 'FM9G999G990D999', 'NLS_NUMERIC_CHARACTERS='',.''') new,
  rtrim(to_char(n, 'FM9G999G990D999', 'NLS_NUMERIC_CHARACTERS='',.'''),
    ',') as trimmed
from t;

         N ORIGINAL      NEW            TRIMMED       
---------- ------------- -------------- --------------
      3.69         3.690 3,69           3,69           
      1000     1,000.000 1.000,         1.000          
     150.2       150.200 150,2          150,2          
         1         1.000 1,             1              
       .16         0.160 0,16           0,16           
like image 142
Alex Poole Avatar answered Sep 27 '22 00:09

Alex Poole