[20211220]sqlplus簡單計算器.txt

lfree發表於2021-12-20

[20211220]sqlplus簡單計算器.txt

--//看了tpt包calc.sql指令碼,他要求輸入3個引數,計算出10進位制以及16進位制結果.
--//但是他的指令碼比較靈活支援10進位制以及16進位制數字的輸入:

--//例子:
SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> @ calc 10 + 4
                                DEC                  HEX
----------------------------------- --------------------
                          14.000000                    E

SCOTT@book> @ calc 0x10 + 4
                                DEC                  HEX
----------------------------------- --------------------
                          20.000000                   14

SCOTT@book> @ calc x10 + 4
                                DEC                  HEX
----------------------------------- --------------------
                          20.000000                   14

--//我自己寫一個簡單一點的輸入10進位制,算出10進位制,16進位制的結果.注意輸入引數中間不能有空格。

$ cat calcx.sql
COL value  new_value newvalue
COL calc_dec HEAD "DEC" FOR 999999999999999999999999999.999999
COL calc_hex HEAD "HEX" FOR A20 JUSTIFY RIGHT

set termout off
select &1 value from dual;
set termout on

select &newvalue calc_dec , TO_CHAR (&newvalue, 'FM0xxxxxxxxxxxxxxx') calc_hex from dual ;

--//測試:
SCOTT@book> @calcx 120/12
                                DEC                  HEX
----------------------------------- --------------------
                          10.000000 000000000000000a


SCOTT@book> @calcx power(2,10)+1
                                DEC                  HEX
----------------------------------- --------------------
                        1025.000000 0000000000000401


--//我以前從網上抄來一個bash計算器,自己修改一下,定義成函式.看看在sqlplus裡面是否支援bash函式呼叫.
--//使用bc計算器,裡面函式不能使用圓括號,使用中括號代替。
--//測試不行。修改成shell。

# cat //usr/local/bin/==
# /bin/bash
# simple calc
in="$(echo "$@" | sed -e 's/\[/(/g' -e 's/\]/)/g')";
echo $in | bc -lq | tr -d '\n\\\r' | sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"
echo

SCOTT@book> host == "scale=4;4/3"
1.3333

--//有分號要加引號才行.

SCOTT@book> host == 2^240
1766847064778384329583297500742918515827483896875618958121606201292619776

--//在bc下執行出現折行情況.

$ echo 2^240 | bc -lq
17668470647783843295832975007429185158274838968756189581216062012926\
19776

SCOTT@book> host == "scale=100;a(1)*4"
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170676

--//PI小數點100位.



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2848491/,如需轉載,請註明出處,否則將追究法律責任。

相關文章