| <!doctype html> |
| <!-- Generated by FreeMarker/Docgen from DocBook --> |
| <html lang="en" class="page-type-section"> |
| <head prefix="og: http://ogp.me/ns#"> |
| <meta charset="utf-8"> |
| <title>Built-ins for numbers - FreeMarker Manual</title> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <meta name="format-detection" content="telephone=no"> |
| <meta property="og:site_name" content="FreeMarker Manual"> |
| <meta property="og:title" content="Built-ins for numbers"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="http://example.com/ref_builtins_number.html"> |
| <link rel="canonical" href="http://example.com/ref_builtins_number.html"> |
| <link rel="icon" href="favicon.png" type="image/png"> |
| <link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338519184"> |
| </head> |
| <body itemscope itemtype="https://schema.org/Code"> |
| <meta itemprop="url" content="http://example.com/"> |
| <meta itemprop="name" content="FreeMarker Manual"> |
| |
| <!--[if lte IE 9]> |
| <div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div> |
| <![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo"> |
| </a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref.html"><span itemprop="name">Reference</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_builtins.html"><span itemprop="name">Built-in Reference</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_builtins_number.html"><span itemprop="name">Built-ins for numbers</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li><li><a href="api/index.html">API</a></li><li><a href="../index.html">Home</a></li></ul></div></div></div> <div class="main-content site-width"> |
| <div class="content-wrapper"> |
| <div id="table-of-contents-wrapper" class="col-left"> |
| <script>var breadcrumb = ["FreeMarker Manual","Reference","Built-in Reference","Built-ins for numbers"];</script> |
| <script src="toc.js?1594338519184"></script> |
| <script src="docgen-resources/main.min.js?1594338519184"></script> |
| </div> |
| <div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="ref_builtins_string.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_builtins_date.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="ref_builtins_number" itemprop="headline">Built-ins for numbers</h1> |
| </div></div><div class="page-menu"> |
| <div class="page-menu-title">Page Contents</div> |
| <ul><li><a class="page-menu-link" href="#ref_builtin_c" data-menu-target="ref_builtin_c">c</a></li><li><a class="page-menu-link" href="#ref_builtin_string_for_number" data-menu-target="ref_builtin_string_for_number">string (when used with a numerical value)</a></li><li><a class="page-menu-link" href="#ref_builtin_rounding" data-menu-target="ref_builtin_rounding">round, floor, ceiling</a></li></ul> </div><p>Related FAQs: Do you have things like 1,000,000 or 1 000 000 |
| instead of 1000000, or something like 3.14 instead of 3,14 or vice |
| versa? See <a href="app_faq.html#faq_number_grouping">this</a> and <a href="app_faq.html#faq_number_decimal_point">this</a> FAQ entry, also note |
| the <code class="inline-code">c</code> built-in above.</p> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_c">c</h2> |
| |
| |
| |
| |
| |
| |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>This built-in exists since FreeMarker 2.3.3.</p> |
| </div> |
| |
| |
| <p>This built-in converts a number to string for ``computer |
| audience'' as opposed to human audience. That is, it formats with |
| the rules that programming languages used to use, which is |
| independent of all the locale and number format settings of |
| FreeMarker. It always uses dot as decimal separator, and it never |
| uses grouping separators (like 3,000,000), nor exponential form |
| (like 5E20), nor superfluous leading or trailing 0-s (like 03 or |
| 1.0), nor + sign (like +1). It will print at most 16 digits after |
| the decimal dot, and thus numbers whose absolute value is less than |
| 1E-16 will be shown as 0. This built-in is crucial because be |
| default (like with <code class="inline-code">${x}</code>) numbers are converted to |
| strings with the locale (language, country) specific number |
| formatting, which is for human readers (like 300000 is possibly |
| printed as 3,000,000). When the number is printed not for human |
| audience (e.g., for a database record ID used as the part of an URL, |
| or as invisible field value in a HTML form, or for printing |
| CSS/JavaScript numerical literals) this built-in must be used to |
| print the number (i.e., use <code class="inline-code">${x?c}</code> instead of |
| <code class="inline-code">${x}</code>), or else the output will be possibly broken |
| depending on the current number formatting settings and locale (like |
| the decimal point is not dot, but comma in many countries) and the |
| value of the number (like big numbers are possibly ``damaged'' by |
| grouping separators).</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_string_for_number">string (when used with a numerical value)</h2> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <p>Converts a number to a string. It uses the default format that |
| the programmer has specified. You can also specify a number format |
| explicitly with this built-in, as it will be shown later.</p> |
| |
| <p>There are four predefined number formats: |
| <code class="inline-code">computer</code>, <code class="inline-code">currency</code>, |
| <code class="inline-code">number</code>, and <code class="inline-code">percent</code>. The exact |
| meaning of these is locale (nationality) specific, and is controlled |
| by the Java platform installation, rather than by FreeMarker, except |
| for <code class="inline-code">computer</code>, which uses the same formatting as |
| <a href="#ref_builtin_c">the <code>c</code> |
| built-in</a>. You can use these predefined formats like |
| this:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#assign x=42> |
| ${x} |
| ${x?string} <#-- the same as ${x} --> |
| ${x?string.number} |
| ${x?string.currency} |
| ${x?string.percent} |
| ${x?string.computer}</pre></div> |
| |
| <p>If your locale is US English, this will certainly |
| produce:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">42 |
| 42 |
| 42 |
| $42.00 |
| 4,200% |
| 42</pre></div> |
| |
| <p>The output of first three expressions is identical because the |
| first two expressions use the default format, which is "number" |
| here. You can change this default using a setting:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#setting number_format="currency"> |
| <#assign x=42> |
| ${x} |
| ${x?string} <#-- the same as ${x} --> |
| ${x?string.number} |
| ${x?string.currency} |
| ${x?string.percent}</pre></div> |
| |
| <p>Will now output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">$42.00 |
| $42.00 |
| 42 |
| $42.00 |
| 4,200%</pre></div> |
| |
| <p>since the default number format was set to "currency".</p> |
| |
| <p>Beside the three predefined formats, you can use arbitrary |
| number format patterns written in <a href="http://java.sun.com/j2se/1.4/docs/api/java/text/DecimalFormat.html">Java |
| decimal number format syntax</a>:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#assign x = 1.234> |
| ${x?string("0")} |
| ${x?string("0.#")} |
| ${x?string("0.##")} |
| ${x?string("0.###")} |
| ${x?string("0.####")} |
| |
| ${1?string("000.00")} |
| ${12.1?string("000.00")} |
| ${123.456?string("000.00")} |
| |
| ${1.2?string("0")} |
| ${1.8?string("0")} |
| ${1.5?string("0")} <-- 1.5, rounded towards even neighbor |
| ${2.5?string("0")} <-- 2.5, rounded towards even neighbor |
| |
| ${12345?string("0.##E0")}</pre></div> |
| |
| <p>outputs this:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">1 |
| 1.2 |
| 1.23 |
| 1.234 |
| 1.234 |
| |
| 001.00 |
| 012.10 |
| 123.46 |
| |
| 1 |
| 2 |
| 2 <-- 1.5, rounded towards even neighbor |
| 2 <-- 2.5, rounded towards even neighbor |
| |
| 1.23E4</pre></div> |
| |
| <p>Following the financial and statistics practice, the rounding |
| goes according the so called half-even rule, which means rounding |
| towards the nearest ``neighbor'', unless both neighbors are |
| equidistant, in which case, it rounds towards the even neighbor. |
| This was visible in the above example if you look at the rounding of |
| 1.5 and of 2.5, as both were rounded to 2, since 2 is even, but 1 |
| and 3 are odds.</p> |
| |
| <p>Appart from the Java decimal syntax patterns, you can also |
| write <code class="inline-code">${aNumber?string("currency")}</code> and like, |
| that will do the same as |
| <code class="inline-code">${aNumber?string.currency}</code> and like.</p> |
| |
| <p>As it was shown for the predefined formats earlier, the |
| default formatting of the numbers can be set in the template:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#setting number_format="0.##"> |
| ${1.234}</pre></div> |
| |
| <p>outputs this:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">1.23</pre></div> |
| |
| <p>Note that the number formatting is locale sensitive:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#setting locale="en_US"> |
| US people write: ${12345678?string(",##0.00")} |
| <#setting locale="hu"> |
| Hungarian people write: ${12345678?string(",##0.00")}</pre></div> |
| |
| <p>outputs this:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">US people write: 12,345,678.00 |
| Hungarian people write: 12 345 678,00</pre></div> |
| |
| <p>You can find information about the formatting of dates <a href="ref_builtins_date.html#ref_builtin_string_for_date">here</a>.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_rounding">round, floor, ceiling</h2> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>The rounding built-ins exist since FreeMarker 2.3.13.</p> |
| </div> |
| |
| |
| <p>Converts a number to a whole number using the specified |
| rounding rule:</p> |
| |
| <ul> |
| <li> |
| <p><code class="inline-code">round</code>: Rounds to the nearest whole |
| number. If the number ends with .5, then it rounds upwards |
| (i.e., towards positive infinity)</p> |
| </li> |
| |
| <li> |
| <p><code class="inline-code">floor</code>: Rounds the number downwards |
| (i.e., towards neagative infinity)</p> |
| </li> |
| |
| <li> |
| <p><code class="inline-code">ceiling</code>: Rounds the number upwards |
| (i.e., towards positive infinity)</p> |
| </li> |
| </ul> |
| |
| <p>Example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#assign testlist=[ |
| 0, 1, -1, 0.5, 1.5, -0.5, |
| -1.5, 0.25, -0.25, 1.75, -1.75]> |
| <#list testlist as result> |
| ${result} ?floor=${result?floor} ?ceiling=${result?ceiling} ?round=${result?round} |
| </#list></pre></div> |
| |
| <p>Prints:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> 0 ?floor=0 ?ceiling=0 ?round=0 |
| 1 ?floor=1 ?ceiling=1 ?round=1 |
| -1 ?floor=-1 ?ceiling=-1 ?round=-1 |
| 0.5 ?floor=0 ?ceiling=1 ?round=1 |
| 1.5 ?floor=1 ?ceiling=2 ?round=2 |
| -0.5 ?floor=-1 ?ceiling=0 ?round=0 |
| -1.5 ?floor=-2 ?ceiling=-1 ?round=-1 |
| 0.25 ?floor=0 ?ceiling=1 ?round=0 |
| -0.25 ?floor=-1 ?ceiling=0 ?round=0 |
| 1.75 ?floor=1 ?ceiling=2 ?round=2 |
| -1.75 ?floor=-2 ?ceiling=-1 ?round=-2</pre></div> |
| |
| <p>These built-ins may be useful in pagination operations and |
| like. If you just want to <em>display</em> numbers in |
| rounded form, then you should rather use the <a href="#ref_builtin_string_for_number"><code>string</code> |
| built-in</a> or the <a href="ref_directive_setting.html#ref.setting.number_format"><code>number_format</code> |
| setting</a>.</p> |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_builtins_string.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_builtins_date.html"><span>Next</span></a></div></div></div></div> </div> |
| </div> |
| <div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"></div><div class="col-right"><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"> <p class="last-generated"> |
| Last generated: |
| <time itemprop="dateModified" datetime="2020-07-09T23:48:39Z" title="Thursday, July 9, 2020 11:48:39 PM GMT">2020-07-09 23:48:39 GMT</time> </p> |
| <p class="copyright"> |
| © <span itemprop="copyrightYear">1999</span>–2020 |
| <a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p> |
| </div></div></div></body> |
| </html> |