doc-exports/docs/dws/dev/dws_06_0010.html
Lu, Huayi e6fa411af0 DWS DEV 830.201 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2024-05-16 07:24:04 +00:00

47 lines
5.8 KiB
HTML

<a name="EN-US_TOPIC_0000001233708691"></a><a name="EN-US_TOPIC_0000001233708691"></a>
<h1 class="topictitle1">Monetary Types</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001233708691__en-us_topic_0059778615_p59509673518">The <strong id="EN-US_TOPIC_0000001233708691__b842352706172427">money</strong> type stores a currency amount with fixed fractional precision. The range shown in <a href="#EN-US_TOPIC_0000001233708691__t46041f911048428d9ee80bf0b323b430">Table 1</a> 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.</p>
<div class="tablenoborder"><a name="EN-US_TOPIC_0000001233708691__t46041f911048428d9ee80bf0b323b430"></a><a name="t46041f911048428d9ee80bf0b323b430"></a><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001233708691__t46041f911048428d9ee80bf0b323b430" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Monetary types</caption><thead align="left"><tr id="EN-US_TOPIC_0000001233708691__r9ad03b8b7e5b4321af76ccdbec2bffbe"><th align="left" class="cellrowborder" valign="top" width="15.040000000000001%" id="mcps1.3.2.2.5.1.1"><p id="EN-US_TOPIC_0000001233708691__a5513e66f3d6d4e028f0304a65bf84187">Name</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="18.42%" id="mcps1.3.2.2.5.1.2"><p id="EN-US_TOPIC_0000001233708691__a548232ad29e943c6ac4732b770bb10d0">Storage Size</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="16.35%" id="mcps1.3.2.2.5.1.3"><p id="EN-US_TOPIC_0000001233708691__a5a7885e85968462b8c8145fbb8f91829">Description</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50.19%" id="mcps1.3.2.2.5.1.4"><p id="EN-US_TOPIC_0000001233708691__a61905c733dc04d2ab357e7ec6967396b">Range</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001233708691__r586163684609437da45984797c648fed"><td class="cellrowborder" valign="top" width="15.040000000000001%" headers="mcps1.3.2.2.5.1.1 "><p id="EN-US_TOPIC_0000001233708691__ab26d599469db40d2850f7652f747439b">money</p>
</td>
<td class="cellrowborder" valign="top" width="18.42%" headers="mcps1.3.2.2.5.1.2 "><p id="EN-US_TOPIC_0000001233708691__a9ccc33550c1f4a7ab4cecf886ab61dc8">8 bytes</p>
</td>
<td class="cellrowborder" valign="top" width="16.35%" headers="mcps1.3.2.2.5.1.3 "><p id="EN-US_TOPIC_0000001233708691__af3eee822ac964d14a15df94736efcc87">Currency amount</p>
</td>
<td class="cellrowborder" valign="top" width="50.19%" headers="mcps1.3.2.2.5.1.4 "><p id="EN-US_TOPIC_0000001233708691__ac15fe3bc67914c2986cf96fd267523a4">-92233720368547758.08 to +92233720368547758.07</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="EN-US_TOPIC_0000001233708691__aaf2f1901fba94408b6e7690561994899">Values of the <strong id="EN-US_TOPIC_0000001233708691__b817361413531">numeric</strong>, <strong id="EN-US_TOPIC_0000001233708691__b617316148538">int</strong>, and <strong id="EN-US_TOPIC_0000001233708691__b017318146538">bigint</strong> data types can be cast to <strong id="EN-US_TOPIC_0000001233708691__b18173161495311">money</strong>. Conversion from the <strong id="EN-US_TOPIC_0000001233708691__b842352706181622">real</strong> and <strong id="EN-US_TOPIC_0000001233708691__b842352706181652">double precision</strong> data types can be done by casting to <strong id="EN-US_TOPIC_0000001233708691__b842352706181717">numeric</strong> first, for example:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708691__s45406dfbc082441799d68299a7364572"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="s1">'12.34'</span><span class="p">::</span><span class="n">float8</span><span class="p">::</span><span class="nb">numeric</span><span class="p">::</span><span class="n">money</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001233708691__a20ad5ad4ead544db8750b6d447f4c10f">However, this is not recommended. Floating point numbers should not be used to handle money due to the potential for rounding errors.</p>
<p id="EN-US_TOPIC_0000001233708691__a22fc7d7f3a1445f5849632dbed0dbc38">A <strong id="EN-US_TOPIC_0000001233708691__b842352706181838">money</strong> value can be cast to <strong id="EN-US_TOPIC_0000001233708691__b842352706181846">numeric</strong> without loss of precision. Conversion to other types could potentially lose precision, and must also be done in two stages:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708691__s9f5d0b6bb97141489e4103c2664076f1"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="s1">'52093.89'</span><span class="p">::</span><span class="n">money</span><span class="p">::</span><span class="nb">numeric</span><span class="p">::</span><span class="n">float8</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001233708691__a920fa6db88044234b4ca2e1fa2431fb6">When a <strong id="EN-US_TOPIC_0000001233708691__b842352706181947">money</strong> value is divided by another <strong id="EN-US_TOPIC_0000001233708691__b842352706181954">money</strong> value, the result is <strong id="EN-US_TOPIC_0000001233708691__b84235270618204">double precision</strong> (that is, a pure number, not money); the currency units cancel each other out in the division.</p>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0008.html">Data Types</a></div>
</div>
</div>