I have a currency amount value like this;
22200000
I want to convert this number to;
22,2 (Number format)
How can I do this?
use to_char() function. Example
to_char(3510.78, '$9,999.00')
would return
$3,510.78
I found the answer: SELECT TO_CHAR (22200000 / 1000000, '999,999,999,999.99') FROM dual
You can use Oracle Built-in function round().
The ROUND function accepts a number and returns another number rounded
to the specified number of places to the right of the decimal point.
If you do not specify that number, ROUND will return a number rounded
to the nearest integer
For instance:
select 1/3, round(1/3, 2) from dual;
1/3 ROUND(1/3,2)
---------- ------------
.333333333 .33
More info: Working with Numbers in PL/SQL
Related
I want to add the decimal precision to be set for the values
Example:
select 1*1.00000;
output: 1.0
Even tried with cast
select cast(cast(1*1.0000 as double) as decimal(5,2))
output: 1
I want the results to be displayed as 1.000. Is there any way to do so in hive?
Create a table and test it. It works if we give the exact precision value as mentioned in the decimal function.
create table test1_decimal (b decimal (5,3));
INSERT INTO test1_Decimal values(1.000); //This will shrink it to 1 as the total digits is not five.
INSERT INTO test1_Decimal values(123.12345); //This results in NULL as it exceeds the total digits(5).
INSERT INTO test1_Decimal values(12.123); //This will give you exact result as the precision and number of digits fits. Ouputs as 12.123
So if the value matches the decimal function then it displays correctly else it shrinks or converts to NULL.
I want to convert this value "2167.124" to "2167.13" using round function in oracle 11g. Problem is that it converts LIKE "2167.12"
But i need "2167.13".
Please help, Thanks in advance
If you want to convert given value 2167.12" to "2167.13" Please use this
select ceil(2167.124*100)/100 from dual;
Expanding on Sam's example, multiplying by 100 effectively moves the decimal point two places to the right, so your value becomes 216712.4:
select 2167.124*100 as step1 from dual;
STEP1
----------
216712.4
You then call ceil on that, to find "the smallest integer that is greater than or equal to n":
select ceil(216712.4) as step2 from dual;
STEP2
----------
216713
Then dividing by 100 effectively moves the decimal point back the same two places to the left:
select 216713/100 as step3 from dual;
STEP3
----------
2167.13
Putting the three steps together into one statement gets:
select ceil(2167.124*100)/100 as result from dual;
RESULT
----------
2167.13
case 1: SELECT TO_CHAR(12345.6789, '99999D99') FROM dual;
Output: 12345.67
case 2: SELECT TO_CHAR(12345.6789, '999D99') FROM dual;
Output: ######
case 3: SELECT TO_CHAR(12345, '99999D99') FROM dual;
Output: 12345.00
case 4: SELECT TO_CHAR(12345.1, '99999D99') FROM dual;
Output: 12345.10
Here Problem is if we don't know how many digits before decimal then how to manage for correct answer.[only case 1,3,4 can resolved using by TO_CHAR but how to solve for case 2.]
In this case the simplest answer might be to not supply a format model at all, but truncate or round the value to two decimal places:
SELECT TO_CHAR(ROUND(12345.6789, 2)) as rounded,
TO_CHAR(TRUNC(12345.6789, 2)) as truncated
FROM dual;
ROUNDED TRUNCATED
-------- ---------
12345.68 12345.67
From the documentation:
If you omit fmt, then n is converted to a VARCHAR2 value exactly long enough to hold its significant digits.
Otherwise you'd need to supply a format model that allowed for the maximum size of your number; if it's unrestricted you'd need 36 nines, the decimal separator, and two more nines. The result would be padded with spaces so you might also want to trim it, depending on how you'll use the string value.
SELECT TO_CHAR(12345.6789, '999999999999999999999999999999999999D99') as val
FROM dual;
VAL
----------------------------------------
12345.68
You could also do that flexibly by using the length of the truncated value (i.e. once the decimal places have been removed):
SELECT TO_CHAR(12345.6789,
lpad('9', length(trunc(12345.6789)), '9') || 'D99') as val
FROM dual;
VAL
---------
12345.68
But that seems unnecessarily complicated when you can let Oracle work it out for you.
However, if you want the decimals to show trailing zeros then you might need to use that method; but with zeros after the decimal separator:
SELECT TO_CHAR(12345.6, lpad('0', length(trunc(12345.6)), '9') || 'D00') as val
FROM dual;
VAL
---------
12345.60
... which addresses the 3rd and 4th cases you added. I've made it show a leading zero for values less than 1 as well; the generated format model in this case is '99990D00'. The number of nines will still vary depending on the size of your number.
By default Oracle still leaves a space at the start for a potential minus sign. You can avoid that with the FM format modifier:
SELECT TO_CHAR(12345.6, 'FM'
|| lpad('0', length(trunc(12345.6)), '9') || 'D00') as val
FROM dual;
VAL
--------
12345.60
You could always go with the maximum number of digits you expect to be present in the input. If there are fewer digits in the input than your format specifier, it wouldn't affect the outcome in anyway. For instance,
select to_char(12323.5553,'99999D99') from dual
would produce,
123.56
As you said, the length of the input is unknwon. So why would you use a fixed length formater for somthing that is unknown? Does not work. Read your input as String from the beginning and manipulate it as String or even better - BLOB.
Well, to do the rounding correct might be tricky.
So, best check your data if the numbers will realy get so big, because that would mean a lot of work and trouble.
If not more than 38 Digits are needed, you can go with decimal or numeric datatype and (if you insist on a formatter) use the TM formatter for example.
SELECT to_char(cast(1234.456 as decimal( *,2)), 'TM') as a FROM dual
or take the advice that was given above by the other posters.
I have a requirement where i always need to display the number with the decimal point.
The datatype of the db column is that of number.
If the value is 1.25 it gets displayed as 1.25
But if the value is 1 it does gets displayed as 1 and I need to display the value as 1.00.
I need to perform rpad (right padding) operations once I get the result in the decimal format.
Without the decimal, the value of a whole number would be different from what is present in the database.
Example:
SELECT RPAD(ROUND(12,2), 5 ,0) AS test
FROM DUAL;
results in 12000 whereas I am expecting it to be 12.00.
Any pointers on this would help.
Thanks!
Use an appropriate to_char call. Something like
SELECT to_char( <<your number>>, '0.00' )
FROM dual;
That will return the string "1.00" when you pass in a value of 1. If you pass in a value of 0, it will return the string "0.00". If you pass in a a value of 1.25, it will return the string "1.25".
Try using a number format along with the TO_CHAR function, as:
SELECT TO_CHAR(12, 99.99) AS test
FROM DUAL;
Reference:
You can find documentatation related to other ways to format numbers here.
Try this:
select TRIM(to_char(100.5, '99999999.00')) FROM DUAL
The format specifications are here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570
This one is pretty simple actually yet I wasn't able to find anything useful.
In my SQL query I have some rounded numbers with a single scale value - round(number,1). If the numbers are rounded to some decimal digit it prints in the format '9,9'.
On the other hand if the numbers are rounded to an integer, only the integer value without the zero after comma is printed although I want my query to select the numbers in '9,9' format even the decimal digit is zero.
In short, I think I need something like for example
to_char((select round(121.01,1), from dual), '*,1') ; to output 121,0.
What is the best way to do this? Thanks in advance
Korhan
All you have to do is specify the number of decimal points you want in your to_char. The problem with using format masks is that you need to specify the number of numbers you want in front of your decimal point.
SQL> select to_char(round(121.01,1),'999.9') from dual;
TO_CHA
------
121.0
SQL> select to_char(round(121.4,1),'999.9') from dual;
TO_CHA
------
121.4
SQL> select to_char(round(121,1),'999.9') from dual;
TO_CHA
------
121.0
SQL> select to_char(round(5121,1),'999.9') from dual;
TO_CHA
------
######
SQL>
There are a number of other formatting options.
Use 0 instead 9 for decimal places:
SELECT TO_CHAR( ROUND( 121.01, 1 ), '990D0' ) num FROM DUAL;
NUM
------
121.0
This simple query may help you,
select to_char(round(121.01,1), '999.0') from dual;
In to_char function:
9 - indicate to block/hide zeros in the output.
0 - indicate to show zero in the output at anywhere in before/after decimal point.
Note:
No. of '9/0's in before/after decimal point is number of digits which you want to display beore/after decimal point.