Template talk:Formatnum

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Patrick brought up two issues with the template in the documentation: rounding errors in some instances and the incompatibility with table sorting.

The rounding errors are not an error in the template, but in the internal representation of the numbers in the parser PHP code (simple float instead of double datatype). The template tries to circumvent its limitations by adding "0.0000000000001". That is too low to invoke rounding errors of its own but fixes many of the rounding errors invoked by the suboptimal datatype. But it cannot fix all of them. If anybody knows how to separate a single digit from a number without doing rounding-error-invoking math, please share your ideas.

The sorting problem should be fixable. I'll try to change the template accordingly soon. --Slomox (talk) 14:20, 30 September 2010 (UTC)

See User:Slomox/test16 for a version of Formatnum that supports a "sortable" parameter to include a sortkey. See the talk page for a small test. Works. If you like it we can update the template. --Slomox (talk) 15:21, 30 September 2010 (UTC)
The rounding problem basically is this: "{{#ifexpr: (0.67 - 0.60) = 0.07 | true | false }}" gives "false" although 0.67 - 0.60 of course _is_ equal to 0.07. Maybe there is a way to fix the error in the parser logic. I just don't know it. --Slomox (talk) 15:44, 30 September 2010 (UTC)
Hm, "{{#ifexpr: 100 + (0.67 - 0.60) = 100 + 0.07 | true | false }}" results in "true". Adding "100" floats the point by a few positions and pushes the floating number impreciseness behind the limit of relevant digits. But I'm not sure if this is a good fix or if it introduces new problems. --Slomox (talk) 16:14, 30 September 2010 (UTC)
Addition: It doesn't need to be "100", "1" is already enough to solve the problem (at least in this case). --Slomox (talk) 16:16, 30 September 2010 (UTC)
It seems we can use (here for 3 decimals):

{{#ifexpr:{{{1}}}>trunc{{{1}}}|{{#expr:(10*{{{1}}})mod10}}{{ #ifexpr:10*{{{1}}}>trunc(10*{{{1}}})|{{#expr:(100*{{{1}}})mod10}}{{ #ifexpr:100*{{{1}}}>trunc(100*{{{1}}})|{{#expr:(1000*{{{1}}})mod10}}}}}}}}

Patrick (talk) 18:40, 30 September 2010 (UTC)

Hm, looks interesting, could work. Could you create a test implementation of Formatnum using the mechanism? (Ideally based on User:Slomox/test16 so we can fix both issues.) --Slomox (talk) 19:15, 30 September 2010 (UTC)
I made m:Template:Formatnum along these lines, but without parameter prec: trailing zeros tend to be associated with rounding, I wonder in what case it would be useful to specify, for example with prec=2, that 12.345 should be left alone while 12.3 should be written 12.30. For sorting we have m:Template:Nts (your sorting does not work, test it with numbers of different numbers of digits), I adapted it to use the new formatnum. There does not seem a need to build a sorting feature into formatnum itself.--Patrick (talk) 12:20, 1 October 2010 (UTC)
I fixed the sorting. (once again one of the inferiorities of MediaWiki hits. Why is there no way to enforce numerical sorting?) --Slomox (talk) 16:18, 1 October 2010 (UTC)
About prec: The problem is "{{Formatnum|12.300}}" which will be rendered as "12.3", which gives the false impression of lack of precision. --Slomox (talk) 16:34, 1 October 2010 (UTC)
Note that like in nts negative numbers are not correctly sorted, and in your method also scientific notation can give complications.
About spec: yes, let me rephrase: the same parameter spec can also be made to provide rounding. --Patrick (talk) 18:26, 1 October 2010 (UTC)
I had a typo in the template. Negative numbers work now.
in your method also scientific notation can give complications: Well, yes. Scientific is completely unsupported by the template Formatnum and I can think of no way to fix that except for implementing a localized version of {{formatnum: }} in Mediawiki.
I still don't get what the problem with spec is. --Slomox (talk) 19:36, 1 October 2010 (UTC)

Your sorting does not work yet:


For sorting positive numbers, even if in scientific notation, see m:Template:Nts. The sortkey contains the list of digits when regular notation is used (found like in the template formatnum). For sorting negative numbers you need for -123 a sortkey containing something like 987.

I fixed some problems in m:Template:Formatnum, what I suggested above is not good enough.

For an explicitly given number to be formatted, prec makes sense by itself, but for a number that is the result of a computation it only makes sense in combination with rounding, so in my version I combined it with rounding.--Patrick (talk) 14:09, 2 October 2010 (UTC)

You can sort scientific notation, but Formatnum is about localizing numbers and there's no way to display "{{Formatnum|3.5e5|hi}}" correctly. Hindi uses a different set of numerals. E.g. "{{Formatnum|3.5|hi}}" is "३.५". (Well, actually "{{Formatnum|3.5e5|hi}}" works, but it won't work if you want to keep the scientific notation.)
I don't know of any way how we can display "{{Formatnum|3.5e5|hi}}" as "३.५e५" (or whatever Hindi uses for scientific notation). So there's no need to support scientific notation in Formatnum. --Slomox (talk) 15:34, 2 October 2010 (UTC)

What I meant was that scientific notation unintendedly produced in the sortkey affects the sorting order:


Patrick (talk) 16:17, 2 October 2010 (UTC)

{{formatnum: |R}}[edit]

Hi, This template doesn't preform a reveres formatnum, I think this can moved to Template:Formatnum/Core and replace by {{Formatnum/Core|{{fomatnum:{{{1|}}}|R}}{{#if:{{{2|}}}|{{!}}2={{{2}}}}} ...}}. this is needed on this case: {{Formatnum|{{formatnum:{{NUMBEROFFILES}}|R}}|fa}}: ۵۹٬۲۷۰٬۳۹۵ −ebraminiotalk 18:04, 31 May 2011 (UTC)

The template assumes that the number is unformatted. If the input is already formatted you have to remove the formatting first. We could make unformatting a build-in feature, but it is really used that often?
You can make your example shorter by using {{NUMBEROFFILES:R}}. --Slomox (talk) 12:56, 3 June 2011 (UTC)
Thank you :) −ebraminiotalk 23:29, 15 June 2011 (UTC)


How I can produce Persian numbers without any separator? {{Formatnum|1000|fa|sep=}}: ۱٬۰۰۰ can not. −ebraminiotalk 18:52, 31 May 2011 (UTC)

You have to set the "sep" parameter to a non-empty value (for example '1'): {{Formatnum|1000|fa|sep=1}}: ۱۰۰۰ --Slomox (talk) 12:49, 3 June 2011 (UTC)
Thanks again :) −ebraminiotalk 23:29, 15 June 2011 (UTC)

Malayalam Numbers[edit]

This template converts digits to Malayalam if using language as Malayalam. Old Malayalam numerals are unreadable for almost all readers. eg: {{formatnum|12345.67}} → ൧൨,൩൪൫.൬൬ . Is there any option to disable this?--Praveen:talk 13:50, 15 June 2011 (UTC)

This is because my edit. I think ml can replaced with ml-old or removed if you want but can you don't use this template instead? In fact I wanted to merge {{CD}} with this template, see {{CD}}s digits table on [1], I extracted that from w3c CSS3 specificationsebraminiotalk 23:29, 15 June 2011 (UTC)

Currently this template is used in many other template including {{Description}}, {{Time}}, {{NoCoins}} etc. Malayalam numerals are archaic and no more commonly used. I feel it is better to change something like ml-old, so that we can use them later, if any case occur (possible ?). --Praveen:talk 11:26, 16 June 2011 (UTC)

Oh, Okay, But it is protected. I can not help :( −ebraminiotalk 14:55, 16 June 2011 (UTC)

requested :)--Praveen:talk 15:45, 16 June 2011 (UTC)

Indian Numbering system[edit]

Is it possible to use Indian numbering system (1234567890 == 1,23,45,67,890) with this template? If possible, how?--Praveen:talk 13:53, 17 June 2011 (UTC)

"{{Formatnum|123456789|bn}}" creates "১২,৩৪,৫৬,৭৮৯". But this is only possible language-wide by creating an entry in {{FormatnumSigns}}, not for a single instance. --Slomox (talk) 09:14, 19 June 2011 (UTC)

I have added Malayalam there. Okay for 9 digits ({{Formatnum|1234567890|ml}} creates 1,23,45,67,890 ; similarly {{Formatnum|1234567890|bn}} creates ১,২৩,৪৫,৬৭,৮৯০) big numbers with 10 digits are very rare, so I think it is not a big problem now. I assume it will be safe adding ta, kn, te, hi, ur, mr, etc there. Please advise --Praveen:talk 09:30, 19 June 2011 (UTC)

And I wonder why this not working?--Praveen:talk 09:37, 19 June 2011 (UTC)
The linked page didn't work, because NUMBEROFFILES returns the number preformatted. You have to use the raw output format NUMBEROFFILES:R.
I corrected the positions of the separator signs for "322". Please check it. --Slomox (talk) 20:24, 19 June 2011 (UTC)
Thank you :). I added more languages to 322 --Praveen:talk 13:24, 20 June 2011 (UTC)


{{Edit request}} Please luafy this template by substituting the bulk of the wikitext in this template (everything except the noinclude tags, and the template transclusion within those) for {{#invoke:Formatnum|main}}. There is an conseus for this change on Commons:Village pump/Proposals#Module:Formatnum.--Snaevar (talk) 23:19, 28 January 2014 (UTC)

Symbol support vote.svg Support, but Can you create {{Formatnum/Sandbox}} and run some tests comparing Lua and current versions first? --Jarekt (talk) 03:34, 29 January 2014 (UTC)
The sandbox is on Template:Formatnum/sandboxlua, the testcases on Module:Formatnum/testcases and the results of the testcases on Module_talk:Formatnum/testcases. I probably should have mentioned that erlier, but assumed that I didn't since it is mentioned on the proposals village pump.--Snaevar (talk) 14:09, 29 January 2014 (UTC)
✓ Done --Jarekt (talk) 14:28, 29 January 2014 (UTC)