PostgreSQL adds trailing zeros to numeric

后端 未结 3 1436
误落风尘
误落风尘 2021-01-12 07:23

Recently I migrated a DB to PostgreSQL that has some columns defined as numeric(9,3) and numeric(9,4). In testing the app I have found that when da

3条回答
  •  感动是毒
    2021-01-12 07:53

    If you specify a precision and scale, Pg pads to that precision and scale.

    regress=> SELECT '0'::NUMERIC(8,4);
     numeric 
    ---------
      0.0000
    (1 row)
    

    There's no way to turn that off. It's still the same number, and the precision is defined by the type, not the value.

    If you want to have the precision defined by the value you have to use unconstrained numeric:

    regress=> SELECT '0'::NUMERIC, '0.0'::NUMERIC;
     numeric | numeric 
    ---------+---------
           0 |     0.0                                                                                                                                                             
    (1 row)                                                                                                                                                                        
    

提交回复
热议问题