Sql 保留兩位小數

2021-09-22 02:31:00 字數 1075 閱讀 6112

sql中欄位保留兩位小數:

使用round()函式,如round(number,2),其中引數2表示保留兩位有效數字,但是只負責四捨五入到兩位小數,但是不負責截斷

例如round(3.141592653, 2)結果為3.140000000;

使用convert(decimal(10,2),number)實現轉換,其中引數2表示保留兩位有效數字 例如convert(decimal(10,2),3.1415)結果為3.14;

使用cast(number as decimal(10,2))實現轉換,其中引數2表示保留兩位有效數字 例如cast(3.1415 as decimal(10,2))結果為3.14;

備註:cast與convert都可以執行資料型別轉換,且都預設實現了四捨五入

如果目標表的字段是decimal(10,4)型的,從源表查資料:select round(field,2) from sourcetable;sql查詢的結果欄位的小數字可能是2位,但是該資料插入目標表,欄位的小數字數就是4位後面2位以0補充;

select decimal(field,10,2) from sourcetable;該資料插入目標表,欄位的小數字就是2位

sql中兩個字段相除並加上%

select

decimal

(field1/field2*

100,10,

2)||'%'

from tablename;

select

round

(field1/field2*

100,2)

||'%'

from tablename;

select concat(

round

(field1/field2*

100,2)

,'%'

)from tablename;

SQL保留兩位小數用法

sql server中除法,會遇到除數為0的情況,且保留兩位小數,寫法如下 select case when num 0 then 0 else convert decimal 18,2 210.343 num end as 結果 from tabnumber select carrier as 承...

保留兩位小數

1.只要求保留n位不四捨5入 float f 0.55555f int i int f 100 f float i 1.0 100 2.保留n位,四捨五入 decimal d decimal.round decimal.parse 0.55555 2 3.保留n位四捨五入 math.round 0....

保留兩位小數

num 10.4567 第一種 利用round 對浮點數進行四捨五入 echo round num,2 第二種 利用sprintf格式化字串 format num sprintf 2f num echo format num 第三種 利用千位分組來格式化數字的函式number format echo...