{"route":"/en-US-v0.14.1/reference/foundations/decimal/","title":"Decimal","description":"Documentation for the Decimal type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"construction-and-casts","name":"Construction and casts","children":[]},{"id":"operations","name":"Operations","children":[]},{"id":"displaying-decimals","name":"Displaying decimals","children":[]},{"id":"precision-and-limits","name":"Precision and limits","children":[]},{"id":"constructor","name":"Constructor","children":[{"id":"constructor-value","name":"value","children":[]}]}],"body":{"kind":"type","content":{"name":"decimal","title":"Decimal","keywords":[],"oneliner":"A fixed-point decimal number type.","details":"<p>A fixed-point decimal number type.</p>\n<p>This type should be used for precise arithmetic operations on numbers\nrepresented in base 10. A typical use case is representing currency.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code>Decimal: <span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;0.1&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">+</span> <span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;0.2&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\nFloat: <span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">0.1</span> <span class=\"typ-op\">+</span> <span class=\"typ-num\">0.2</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/5b7d4abe1e81bdf2204e021eab6b8811.png\" alt=\"Preview\"></div></div>\n<h2 id=\"construction-and-casts\">Construction and casts</h2>\n<p>To create a decimal number, use the <code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span>string<span class=\"typ-punct\">)</span></code> constructor, such as\nin <code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;3.141592653&quot;</span><span class=\"typ-punct\">)</span></code> <strong>(note the double quotes!)</strong>. This\nconstructor preserves all given fractional digits, provided they are\nrepresentable as per the limits specified below (otherwise, an error is\nraised).</p>\n<p>You can also convert any <a href=\"/en-US-v0.14.1/reference/foundations/int/\">integer</a> to a decimal with the\n<code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span>int<span class=\"typ-punct\">)</span></code> constructor, e.g. <code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">59</span><span class=\"typ-punct\">)</span></code>. However, note that\nconstructing a decimal from a <a href=\"/en-US-v0.14.1/reference/foundations/float/\">floating-point number</a>, while\nsupported, <strong>is an imprecise conversion and therefore discouraged.</strong> A\nwarning will be raised if Typst detects that there was an accidental <code>float</code>\nto <code>decimal</code> cast through its constructor, e.g. if writing <code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">3.14</span><span class=\"typ-punct\">)</span></code>\n(note the lack of double quotes, indicating this is an accidental <code>float</code>\ncast and therefore imprecise). It is recommended to use strings for\nconstant decimal values instead (e.g. <code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;3.14&quot;</span><span class=\"typ-punct\">)</span></code>).</p>\n<p>The precision of a <code>float</code> to <code>decimal</code> cast can be slightly improved by\nrounding the result to 15 digits with <a href=\"/en-US-v0.14.1/reference/foundations/calc/#functions-round\" title=\"`calc.round`\"><code>calc.round</code></a>, but there are still no\nprecision guarantees for that kind of conversion.</p>\n<h2 id=\"operations\">Operations</h2>\n<p>Basic arithmetic operations are supported on two decimals and on pairs of\ndecimals and integers.</p>\n<p>Built-in operations between <code>float</code> and <code>decimal</code> are not supported in order\nto guard against accidental loss of precision. They will raise an error\ninstead.</p>\n<p>Certain <code>calc</code> functions, such as trigonometric functions and power between\ntwo real numbers, are also only supported for <code>float</code> (although raising\n<code>decimal</code> to integer exponents is supported). You can opt into potentially\nimprecise operations with the <code><span class=\"typ-func\">float</span><span class=\"typ-punct\">(</span>decimal<span class=\"typ-punct\">)</span></code> constructor, which casts\nthe <code>decimal</code> number into a <code>float</code>, allowing for operations without\nprecision guarantees.</p>\n<h2 id=\"displaying-decimals\">Displaying decimals</h2>\n<p>To display a decimal, simply insert the value into the document. To only\ndisplay a certain number of digits, <a href=\"/en-US-v0.14.1/reference/foundations/calc/#functions-round\">round</a> the decimal first.\nLocalized formatting of decimals and other numbers is not yet supported, but\nplanned for the future.</p>\n<p>You can convert decimals to strings using the <a href=\"/en-US-v0.14.1/reference/foundations/str/\" title=\"`str`\"><code>str</code></a> constructor. This way,\nyou can post-process the displayed representation, e.g. to replace the\nperiod with a comma (as a stand-in for proper built-in localization to\nlanguages that use the comma).</p>\n<h2 id=\"precision-and-limits\">Precision and limits</h2>\n<p>A <code>decimal</code> number has a limit of 28 to 29 significant base-10 digits. This\nincludes the sum of digits before and after the decimal point. As such,\nnumbers with more fractional digits have a smaller range. The maximum and\nminimum <code>decimal</code> numbers have a value of <code><span class=\"typ-num\">79228162514264337593543950335</span></code>\nand <code><span class=\"typ-op\">-</span><span class=\"typ-num\">79228162514264337593543950335</span></code> respectively. In contrast with\n<a href=\"/en-US-v0.14.1/reference/foundations/float/\" title=\"`float`\"><code>float</code></a>, this type does not support infinity or NaN, so overflowing or\nunderflowing operations will raise an error.</p>\n<p>Typical operations between <code>decimal</code> numbers, such as addition,\nmultiplication, and <a href=\"/en-US-v0.14.1/reference/foundations/calc/#functions-pow\">power</a> to an integer, will be highly precise\ndue to their fixed-point representation. Note, however, that multiplication\nand division may not preserve all digits in some edge cases: while they are\nconsidered precise, digits past the limits specified above are rounded off\nand lost, so some loss of precision beyond the maximum representable digits\nis possible. Note that this behavior can be observed not only when dividing,\nbut also when multiplying by numbers between 0 and 1, as both operations can\npush a number's fractional digits beyond the limits described above, leading\nto rounding. When those two operations do not surpass the digit limits, they\nare fully precise.</p>","constructor":{"path":[],"name":"decimal","title":"Construct","keywords":[],"oneliner":"Converts a value to a `decimal`.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Converts a value to a <code>decimal</code>.</p>\n<p>It is recommended to use a string to construct the decimal number, or an\n<a href=\"/en-US-v0.14.1/reference/foundations/int/\">integer</a> (if desired). The string must contain a number in the\nformat <code><span class=\"typ-str\">&quot;3.14159&quot;</span></code> (or <code><span class=\"typ-str\">&quot;-3.141519&quot;</span></code> for negative numbers). The\nfractional digits are fully preserved; if that's not possible due to the\nlimit of significant digits (around 28 to 29) having been reached, an\nerror is raised as the given decimal number wouldn't be representable.</p>\n<p>While this constructor can be used with <a href=\"/en-US-v0.14.1/reference/foundations/float/\">floating-point numbers</a>\nto cast them to <code>decimal</code>, doing so is <strong>discouraged</strong> as <strong>this cast is\ninherently imprecise.</strong> It is easy to accidentally perform this cast by\nwriting <code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">1.234</span><span class=\"typ-punct\">)</span></code> (note the lack of double quotes), which is\nwhy Typst will emit a warning in that case. Please write\n<code><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;1.234&quot;</span><span class=\"typ-punct\">)</span></code> instead for that particular case (initialization of\na constant decimal). Also note that floats that are NaN or infinite\ncannot be cast to decimals and will raise an error.</p>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">decimal</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;1.222222222222222&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/45faa507ce50e6521579e6c9abb4659a.png\" alt=\"Preview\"></div></div>","title":null}}],"self":false,"params":[{"name":"value","details":[{"kind":"html","content":"<p>The value that should be converted to a decimal.</p>"}],"types":["bool","int","float","str","decimal"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["decimal"],"scope":[]},"scope":[]}}}