gpt4 book ai didi

postgresql - Postgres 自定义浮点类型,它总是在点后截断为 2 位小数

转载 作者:行者123 更新时间:2023-11-29 14:15:02 30 4
gpt4 key购买 nike

我能否在 postgres 中生成一个自定义数据类型,每次我向其中插入或更新一个 float 时,它都会在点后被截断为 2 位小数。

create table money(

formatted moneys_type
);

insert into money values (30.122323213);

Select * from money;

返回

30.12

更新我没有使用数字或小数,因为它们在 1.999 => 2 时四舍五入

最佳答案

参见 documentation on Numeric Types / Arbitrary Precision Numbers .

The precision of a numeric is the total count of significant digits inthe whole number, that is, the number of digits to both sides of thedecimal point. The scale of a numeric is the count of decimal digitsin the fractional part, to the right of the decimal point. So thenumber 23.5141 has a precision of 6 and a scale of 4. Integers can beconsidered to have a scale of zero.

...

To declare a column of type numeric use the syntax:

NUMERIC(precision, scale)

The maximum allowed precision when explicitly specified in the type declaration is 1000.

所以你可以使用

NUMERIC(1000, 2)

关于postgresql - Postgres 自定义浮点类型,它总是在点后截断为 2 位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52173306/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com