Home » SQL & PL/SQL » SQL & PL/SQL » Format a String to 2 decimal place
Format a String to 2 decimal place [message #618721] Tue, 15 July 2014 01:57 Go to next message
jimjohn
Messages: 12
Registered: July 2014
Junior Member
Hi All,

I have 15 digit number stored as varchar2.
I want to decimal format this number to 2 digits.
How can i achieve this.
Ex:
input out put
000000000004166 41.66
000000000009475 94.75
120000000009466 1200000000094.66
Re: Format a String to 2 decimal place [message #618723 is a reply to message #618721] Tue, 15 July 2014 01:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

to_number(val)/100

Re: Format a String to 2 decimal place [message #618724 is a reply to message #618721] Tue, 15 July 2014 02:00 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
TO_NUMBER(input) / 100
Re: Format a String to 2 decimal place [message #618726 is a reply to message #618724] Tue, 15 July 2014 02:08 Go to previous messageGo to next message
jimjohn
Messages: 12
Registered: July 2014
Junior Member
Thanks All.It works out
Re: Format a String to 2 decimal place [message #618727 is a reply to message #618726] Tue, 15 July 2014 02:10 Go to previous messageGo to next message
jimjohn
Messages: 12
Registered: July 2014
Junior Member
If the input is like this i will get only 000000000030000 30 as output.Can I have 30.00 as output
Re: Format a String to 2 decimal place [message #618728 is a reply to message #618727] Tue, 15 July 2014 02:12 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Yes; apply TO_CHAR with appropriate format mask.
Re: Format a String to 2 decimal place [message #618736 is a reply to message #618728] Tue, 15 July 2014 02:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

With the link: TO_CHAR.

Re: Format a String to 2 decimal place [message #618741 is a reply to message #618736] Tue, 15 July 2014 02:52 Go to previous messageGo to next message
jimjohn
Messages: 12
Registered: July 2014
Junior Member
Thanks all.I solved
Re: Format a String to 2 decimal place [message #618747 is a reply to message #618741] Tue, 15 July 2014 03:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It is appreciated you post the final solution to help future readers.

Re: Format a String to 2 decimal place [message #618762 is a reply to message #618747] Tue, 15 July 2014 05:02 Go to previous messageGo to next message
jimjohn
Messages: 12
Registered: July 2014
Junior Member
Here is my solution.

to_char( to_number(AMOUNT/100),'FM9999999.90')
Re: Format a String to 2 decimal place [message #618763 is a reply to message #618762] Tue, 15 July 2014 05:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Thank you.

Re: Format a String to 2 decimal place [message #618783 is a reply to message #618762] Tue, 15 July 2014 08:59 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
jimjohn wrote on Tue, 15 July 2014 06:02
Here is my solution.

to_char( to_number(AMOUNT/100),'FM9999999.90')



Look at to_number(AMOUNT/100). Amount is a string. So AMOUNT/100 implicitly converts AMOUNT to number and then divides it by 100. Then TO_NUMBER which expects a string implicitly converts number AMOUNT/100 to string and then back to number. Do you see how many unnecessary conversions code performs? Compare it with:

to_char(AMOUNT / 100,'FM9999999.90')


But you could use REGEXP_REPLACE which could be faster than converting string to number then dividing by 100 and then converting it back to string:

regexp_replace(amount,'^0*(.+)(..)$','\1.\2')


and if amount can be negative:

regexp_replace(amount,'^(-)?0*(.+)(..)$','\1\2.\3')


SY.

[Updated on: Tue, 15 July 2014 09:01]

Report message to a moderator

Previous Topic: Execute immediate
Next Topic: oracle 11g along with XML
Goto Forum:
  


Current Time: Wed Apr 24 09:26:04 CDT 2024