8.2. Monetary Types

8.2. Monetary Types
8.2. Currency Type
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.
The type of currency to a fixed monetary amount stored fractional accuracy; see Table 8.3. Lc_monetary decimal precision determined by the setting of the database. Table displayed range Suppose there are two decimal places. There are many acceptable input formats, including integer and floating-point text, as well as typical currency formatting, such as "$ 1,000.00." After the output of one form usually, but depends on the locale.
 
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.
Since this type of data is output locale-sensitive, so to be loaded with money data may be problematic in the database with a different set of lc_monetary. To avoid problems, before restoring, make sure that the value lc_monetary the same or equivalent in the old and new library.
 
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 values ​​and money type can be cast. The real and double types can be converted into numeric then converted to the money type. E.g:
 
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.
However, this operation is not recommended. We do not recommend the use of floating-point data types when handling money, because there will be problems when rounding.
 
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 value may not be lost precision cast to numeric data types. Conversion to other types may lose precision, but also must take two steps:
 
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.
The monetary value by integer values, the fractional part will be truncated to zero. If you want to get the result of rounding, please divided by floating point value, or convert currency values ​​to numeric type, then removed, then returned to the money type. (Which is best avoided in order to avoid the risk of suffering a loss of precision.) When a monetary value will be divided by another monetary value, the result is a double type (i.e. purely digital, rather than monetary value); currency units to each other in the division offset.
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

Guess you like

Origin blog.csdn.net/ghostliming/article/details/104614234