8.2. Monetary Types

8.2. Monetary Types
8.2.货币类型
The money type stores a currency amount with a fixed fractional precision; see Table 8.3. The fractional precision is determined by the database's lc_monetary setting. The range shown in the table assumes there are two fractional digits. Input is accepted in a variety of formats, including integer and floating-point literals, as well as typical currency formatting, such as '$1,000.00'. Output is generally in the latter form but depends on the locale.
货币类型以固定的分数精度存储货币金额;参见表8.3。小数精度由数据库的lc_monetary设置决定。表中显示的范围假设有两个小数位。接受的输入格式有多种,包括整数和浮点文字,以及典型的货币格式,例如“ $ 1,000.00”。输出通常采用后一种形式,但取决于语言环境。
 
Since the output of this data type is locale-sensitive, it might not work to load money data into a database that has a different setting of lc_monetary. To avoid problems, before restoring a dump into a new database make sure lc_monetary has the same or equivalent value as in the database that was dumped.
由于此数据类型的输出是对语言环境敏感的,因此将money数据加载到具有lc_monetary不同设置的数据库中可能会有问题。为避免问题,在还原之前,请确保lc_monetary的值在新旧库中相同或等效。
 
Values of the numeric, int, and bigint data types can be cast to money. Conversion from the real and double precision data types can be done by casting to numeric first, for example:
numeric、int和bigint值可以强制转换为money类型。real和double类型可以先转换到numeric再转换到money类型。例如:
 
SELECT '12.34'::float8::numeric::money;
 
However, this is not recommended. Floating point numbers should not be used to handle money due to the potential for rounding errors.
然而,不建议这样操作。处理货币数据时不建议使用浮点数类型,因为在四舍五入时会存在问题。
 
A money value can be cast to numeric without loss of precision. Conversion to other types could potentially lose precision, and must also be done in two stages:
money值可以不丢失精度的强制转换成numeric数据类型。转换到其他类型可能会丢失精度,而且还必须两步走:
 
SELECT '52093.89'::money::numeric::float8;
 
Division of a money value by an integer value is performed with truncation of the fractional part towards zero. To get a rounded result, divide by a floating-point value, or cast the money value to numeric before dividing and back to money afterwards. (The latter is preferable to avoid risking precision loss.) When a money value is divided by another money value, the result is double precision (i.e., a pure number, not money); the currency units cancel each other out in the division.
将货币值除以整数值,会将小数部分截断为零。如果想要获得四舍五入的结果,请除以浮点值,或将货币值转换为numeric类型,然后再除,然后再返回至money类型。(最好避免使用后者,以免遭受精度损失的风险。)将一个货币值除以另一个货币值时,结果是双精度类型(即纯数字,而不是货币值); 货币单位在除法中彼此抵消了。
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104614234
8.2