{"id":2281,"date":"2021-02-26T06:04:05","date_gmt":"2021-02-26T06:04:05","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2281"},"modified":"2021-02-26T06:08:24","modified_gmt":"2021-02-26T06:08:24","slug":"python-decimal","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-decimal\/","title":{"rendered":"Python Decimal"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>decimal<\/code> module that supports fast correctly-rounded decimal floating-point arithmetic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-decimal-module'>Introduction to the Python decimal module <a href=\"#introduction-to-the-python-decimal-module\" class=\"anchor\" id=\"introduction-to-the-python-decimal-module\" title=\"Anchor for Introduction to the Python decimal module\">#<\/a><\/h2>\n\n\n\n<p>Many decimal numbers don&#8217;t have exact representations in binary <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">floating-point<\/a> such as 0.1. When using these numbers in arithmetic operations,  you&#8217;ll get a result that you would not expect. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">x = <span class=\"hljs-number\">0.1<\/span>\ny = <span class=\"hljs-number\">0.1<\/span>\nz = <span class=\"hljs-number\">0.1<\/span>\n\ns = x + y + z\n\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">0<span class=\"hljs-selector-class\">.30000000000000004<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The result is 0.30000000000000004, not 0.3.<\/p>\n\n\n\n<p>To solve this problem, you use the <code>Decimal<\/code> class from the <code>decimal<\/code> module as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> decimal\n<span class=\"hljs-keyword\">from<\/span> decimal <span class=\"hljs-keyword\">import<\/span> Decimal\n\n\nx = Decimal(<span class=\"hljs-string\">'0.1'<\/span>)\ny = Decimal(<span class=\"hljs-string\">'0.1'<\/span>)\nz = Decimal(<span class=\"hljs-string\">'0.1'<\/span>)\n\ns = x + y + z\n\nprint(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">0<span class=\"hljs-selector-class\">.3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output is as expected.<\/p>\n\n\n\n<p>The Python <code>decimal<\/code> module supports arithmetic that works the same as the arithmetic you learn at school.<\/p>\n\n\n\n<p>Unlike <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">floats<\/a>, Python represents decimal numbers exactly. And the exactness carries over into arithmetic. For example, the following expression returns exactly 0.0:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Decimal(<span class=\"hljs-string\">'0.1'<\/span>) + Decimal(<span class=\"hljs-string\">'0.1'<\/span>) + Decimal(<span class=\"hljs-string\">'0.1'<\/span>) - Decimal(<span class=\"hljs-string\">'0.3'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='decimal-context'>Decimal context <a href=\"#decimal-context\" class=\"anchor\" id=\"decimal-context\" title=\"Anchor for Decimal context\">#<\/a><\/h2>\n\n\n\n<p>Decimal always associates with a <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-context-managers\/\">context<\/a> that controls the following aspects:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Precision during an arithmetic operation<\/li><li>Rounding algorithm<\/li><\/ul>\n\n\n\n<p>By default, the context is global. The global context is the default context. Also, you can set a temporary context that will take effect locally without affecting the global context.<\/p>\n\n\n\n<p>To get the default context, you call the <code>getcontext()<\/code> function from the <code>decimal<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">decimal<\/span><span class=\"hljs-selector-class\">.getcontext<\/span>()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>getcontext()<\/code> function returns the default context, which can be global or local.<\/p>\n\n\n\n<p>To create a new context copied from another context, you use the <code>localcontext()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">decimal.localcontext(ctx=None)<\/code><\/span><\/pre>\n\n\n<p>The <code>localcontext()<\/code> returns a new context copied from the context <code>ctx<\/code> if specified.<\/p>\n\n\n\n<p>Once getting the context object, you can access the precision and rouding via the <code>prec<\/code> and <code>rounding<\/code> property respectively:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>ctx.pre<\/code>: get or set the precision. The ctx.pre is an integer which defaults to 28<\/li><li><code>ctx.rounding<\/code>: get or set the rounding mechanism. The rounding is a string. It defaults to <code>'ROUND_HALF_EVEN'<\/code>. Note floats also use this rounding mechanism.<\/li><\/ul>\n\n\n\n<p>Python provides the following rounding mechanisms:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Rounding<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>ROUND_UP<\/td><td>round away from zero<\/td><\/tr><tr><td>ROUND_DOWN<\/td><td>round towards zero<\/td><\/tr><tr><td>ROUND_CEILING<\/td><td>round to ceiling (towards positive infinity)<\/td><\/tr><tr><td>ROUND_FLOOR<\/td><td>round to floor (towards negative infinity)<\/td><\/tr><tr><td>ROUND_HALF_UP<\/td><td>round to nearest, ties away from zero<\/td><\/tr><tr><td>ROUND_HALF_DOWN<\/td><td>round to nearest, ties towards zero<\/td><\/tr><tr><td>ROUND_HALF_EVEN<\/td><td>round to nearest, ties to even (least significant digit)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This example illustrates how to get the default precision and rounding of the default context:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import decimal\n\nctx = decimal.getcontext()\n\n<span class=\"hljs-keyword\">print<\/span>(ctx.prec)\n<span class=\"hljs-keyword\">print<\/span>(ctx.rounding)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">28\nROUND_HALF_EVEN<\/code><\/span><\/pre>\n\n\n<p>The following example shows how the <code>'ROUND_HALF_EVEN'<\/code> rounding mechanism takes effect:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> decimal\n<span class=\"hljs-keyword\">from<\/span> decimal <span class=\"hljs-keyword\">import<\/span> Decimal\n\n\nx = Decimal(<span class=\"hljs-string\">'2.25'<\/span>)\ny = Decimal(<span class=\"hljs-string\">'3.35'<\/span>)\n\nprint(round(x, <span class=\"hljs-number\">1<\/span>))\nprint(round(y, <span class=\"hljs-number\">1<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">2<span class=\"hljs-selector-class\">.2<\/span>\n3<span class=\"hljs-selector-class\">.4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you change the rounding to <code>'ROUND_HALF_UP'<\/code>, you&#8217;ll get a different result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> decimal\n<span class=\"hljs-keyword\">from<\/span> decimal <span class=\"hljs-keyword\">import<\/span> Decimal\n\n\nctx = decimal.getcontext()\nctx.rounding = decimal.ROUND_HALF_UP\n\nx = Decimal(<span class=\"hljs-string\">'2.25'<\/span>)\ny = Decimal(<span class=\"hljs-string\">'3.35'<\/span>)\n\nprint(round(x, <span class=\"hljs-number\">1<\/span>))\nprint(round(y, <span class=\"hljs-number\">1<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">2<span class=\"hljs-selector-class\">.3<\/span>\n3<span class=\"hljs-selector-class\">.4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows you how to copy the default context and change the rounding to <code>'ROUND_HALF_UP'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import decimal\nfrom decimal import Decimal\n\n\nx = Decimal(<span class=\"hljs-string\">'2.25'<\/span>)\ny = Decimal(<span class=\"hljs-string\">'3.35'<\/span>)\n\nwith decimal.localcontext() <span class=\"hljs-keyword\">as<\/span> ctx:\n    <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'Local context:'<\/span>)\n    ctx.rounding = decimal.ROUND_HALF_UP\n    <span class=\"hljs-keyword\">print<\/span>(round(x, <span class=\"hljs-number\">1<\/span>))\n    <span class=\"hljs-keyword\">print<\/span>(round(y, <span class=\"hljs-number\">1<\/span>))\n\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'Global context:'<\/span>)\n<span class=\"hljs-keyword\">print<\/span>(round(x, <span class=\"hljs-number\">1<\/span>))\n<span class=\"hljs-keyword\">print<\/span>(round(y, <span class=\"hljs-number\">1<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">Local<\/span> <span class=\"hljs-selector-tag\">context<\/span>:\n2<span class=\"hljs-selector-class\">.3<\/span>\n3<span class=\"hljs-selector-class\">.4<\/span>\n<span class=\"hljs-selector-tag\">Global<\/span> <span class=\"hljs-selector-tag\">context<\/span>:\n2<span class=\"hljs-selector-class\">.2<\/span>\n3<span class=\"hljs-selector-class\">.4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the local context doesn&#8217;t affect the global context. After the with block, Python uses the default rounding mechanism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='decimal-constructor'>Decimal constructor <a href=\"#decimal-constructor\" class=\"anchor\" id=\"decimal-constructor\" title=\"Anchor for Decimal constructor\">#<\/a><\/h2>\n\n\n\n<p>The <code>Decimal<\/code> constructor allows you to create a new <code>Decimal<\/code> object based on a value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Decimal(value=<span class=\"hljs-string\">'0'<\/span>, context=None)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>value<\/code> argument can be an integer, string, tuple, float, or another Decimal object. If you don&#8217;t provide the value argument, it defaults to <code>'0'<\/code>.<\/p>\n\n\n\n<p>If the value is a tuple, it should have three components: a sign (0 for positive or 1 for negative), a tuple of digits, and an integer exponent:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">(sign, (digit1,digit2, digit3,...), exponent)<\/code><\/span><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">3.14 = 314 x 10^-2<\/code><\/span><\/pre>\n\n\n<p>The tuple has three elements as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>sign is 0<\/li><li>digits is (3,1,4)<\/li><li>exponent is -2<\/li><\/ul>\n\n\n\n<p>Therefore, you&#8217;ll need to pass the following tuple to the <code>Decimal<\/code> constructor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> decimal\n<span class=\"hljs-keyword\">from<\/span> decimal <span class=\"hljs-keyword\">import<\/span> Decimal\n\nx = Decimal((<span class=\"hljs-number\">0<\/span>, (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">4<\/span>), <span class=\"hljs-number\">-2<\/span>))\nprint(x)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">3<span class=\"hljs-selector-class\">.14<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the decimal context precision only affects the arithmetic operation, not the <code>Decimal<\/code> constructor. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> decimal\n<span class=\"hljs-keyword\">from<\/span> decimal <span class=\"hljs-keyword\">import<\/span> Decimal\n\n\ndecimal.getcontext().prec = <span class=\"hljs-number\">2<\/span>\n\npi = Decimal(<span class=\"hljs-string\">'3.14159'<\/span>)\nradius = <span class=\"hljs-number\">1<\/span>\n\nprint(pi)\n\narea = pi * radius * radius\nprint(area)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you use a float that doesn&#8217;t have an exact binary float representation, the <code>Decimal<\/code> constructor cannot create an accurate decimal representation. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> decimal\n<span class=\"hljs-keyword\">from<\/span> decimal <span class=\"hljs-keyword\">import<\/span> Decimal\n\nx = Decimal(<span class=\"hljs-number\">0.1<\/span>)\nprint(x)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">0<span class=\"hljs-selector-class\">.1000000000000000055511151231257827021181583404541015625<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In practice, you&#8217;ll use a string or a tuple to construct a <code>Decimal<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='decimal-arithmetic-operations'>Decimal arithmetic operations <a href=\"#decimal-arithmetic-operations\" class=\"anchor\" id=\"decimal-arithmetic-operations\" title=\"Anchor for Decimal arithmetic operations\">#<\/a><\/h2>\n\n\n\n<p>Some arithmetic operators don&#8217;t work the same as floats or <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-integers\/\">integers<\/a>, such as <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-floor-division\/\">div<\/a> (<code>\/\/<\/code>) and mod (<code>%)<\/code>.<\/p>\n\n\n\n<p>For decimal numbers, the <code>\/\/<\/code> operator performs a truncated division:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">x <span class=\"hljs-comment\">\/\/ y = trunc( x \/ y)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Decimal<\/code> class provides some mathematical operations such as <code>sqrt<\/code> and <code>log<\/code>. However, it doesn&#8217;t have all the functions defined in the <code>math<\/code> module.<\/p>\n\n\n\n<p>When you use functions from the <code>math<\/code> module for decimal numbers, Python will cast the <code>Decimal<\/code> objects to floats before carrying arithmetic operations. This results in losing the precision built in the decimal objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the Python <code>decimal<\/code> module when you want to support fast correctly-rounded decimal floating-point arithmetic.<\/li><li>Use the <code>Decimal<\/code> class from the <code>decimal<\/code> module to create Decimal object from strings, integers, and tuples.<\/li><li>The <code>Decimal<\/code> numbers have a context that controls the precision and rounding mechanism.<\/li><li>The <code>Decimal<\/code> class doesn&#8217;t have all methods defined in the <code>math<\/code> module. However, you should use the Decimal&#8217;s arithmetic methods if they&#8217;re available.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2281\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-decimal\/\"\n\t\t\t\tdata-post-title=\"Python Decimal\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2281\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-decimal\/\"\n\t\t\t\tdata-post-title=\"Python Decimal\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python decimal module that supports fast correctly-rounded decimal floating-point arithmetic<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":33,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2281","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2281"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2281\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}