postgreSQL保留小數

2021-08-08 09:56:03 字數 1203 閱讀 2430

--1 例子

postgres=# select 1/4;

?column? 

----------

0(1 row)

在pg裡如果想做除法並想保留小數,用上面的方法卻行不通,因為"/" 運算結果為取整,並且會截掉小數部分。

--2 型別轉換

postgres=# select round(1::numeric/4::numeric,2);

round 

-------

0.25

(1 row)

備註:型別轉換後,就能保留小數部分了。

--3 也可以通過 cast 函式進行轉換

postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);

round 

-------

0.25

(1 row)

--4 關於 cast 函式的用法

postgres=# select substr(cast (1234 as text), 3,1);

substr 

--------

3(1 row)--1 例子

postgres=# select 1/4;

?column? 

----------

0(1 row)

在pg裡如果想做除法並想保留小數,用上面的方法卻行不通,因為"/" 運算結果為取整,並且會截掉小數部分。

--2 型別轉換

postgres=# select round(1::numeric/4::numeric,2);

round 

-------

0.25

(1 row)

備註:型別轉換後,就能保留小數部分了。

--3 也可以通過 cast 函式進行轉換

postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);

round 

-------

0.25

(1 row)

--4 關於 cast 函式的用法

postgres=# select substr(cast (1234 as text), 3,1);

substr 

--------

3(1 row)

scala 保留小數

scala 中的double型別資料在輸出到csv或其他資料檔案時,如果小數字過長會出現類似於3.573914050694557e 4這樣的數值,這在讀取該資料檔案時處理非常不方便。為了解決該問題,可在儲存檔案前就將該資料格式進行轉換,只保留指定位的小數。方法1 val pi double scal...

大數相除 保留小數

大數相除,保留小數後18位。例如 6666666666666 555555555555555 0.011999999999998812 輸入 如例子所示的兩個 大數 用空格隔開。數字範圍在 10 20 位之間。輸出 計算結果 小數點後面保留 18 位 include include using na...

JS保留小數方法

js保留小數的方法如下 以保留兩位為例 1 tofixed 方法 需注意,保留兩位小數,將數值型別的資料改變成了字串型別 1.四捨五入 var num 1.7321 num num.tofixed 2 console.log num console.log typeof num string2 ma...