Template talk:Round

Rnd uses too many subtemplates for Convert
04-April-2010: The standard Template:Convert has been hitting template-nesting limits, when used inside nested infoboxes or nested doc subpages. For some conversions, Convert has been using over 23 nested subtemplates. A major part of the too-many subtemplates is the use of the unneeded middle-man subtemplate Rnd/a for rounding almost every conversion result. Hence, see topic below: . Every subtemplate which can be bypassed is another less for worry. -Wikid77 (talk) 15:56, 4 April 2010 (UTC)

Condensing Rnd by combining Rnd/a
04-April-2010: The rounding Template:Rnd can be condensed by merging the short contents of subtemplate Rnd/a and avoid an extra level of nested subtemplates. Removing 1 more nest-level can help avoid hitting Wikipedia's template-nesting limit. Rnd can be changed to directly invoke Rnd/b0, Rnd/b1 or Rnd/b-1 without using the "middle-man" Template:Rnd/a. The condensed template would be identical to Rnd invoking Rnd/a, but avoids the 2-step transclusion by combining expressions formerly split between the 2 templates. As shown by the template contents, below, the proposed version of {Rnd} would be similar in size to combining the text of the former {Rnd} with {Rnd/a}.

The proposed Template:Rnd replaces former Template:Rnd & Template:Rnd/a combined, as just one level of nesting to perform the same steps.

Template:Rnd:     (proposed version)
 * 0

Template:Rnd:     (former version)
 * 0

Template:Rnd/a:      (formerly used)

As shown above, the proposed contents of Template:Rnd would be similar in size to combining the text of the two templates, since the joining of the 2 allows combining the 3 {#expr} expressions, which, formerly, had been split between the 2 templates. The supposed efficiency of passing common parameters, into Rnd/a, had been offset by the inefficiency of comparing those parameters in multiple expressions, which had been split between the 2 templates. By re-combining the 2 templates, the split expressions can be re-combined, and hence the reduced template is also efficient. There had been no real improvement by splitting into {Rnd/a}, but fortunately, recombining {Rnd/a} into {Rnd} is a simple change. -Wikid77 (talk) 15:56, 4 April 2010 (UTC)

Change Rnd to bypass Rnd/a
05-April-2010: The rounding Template:Rnd needs to be replaced from the Template:Rnd/sandbox to combine the contents of Rnd/a and allow that subtemplate to be bypassed, as 1 less nesting level against the Wikipedia template-nesting limit. IMPACT: nearly 307,000 pages will be reformatted, with over 286,000 using {Convert}, while the other pages use rounding in other calculations.

The condensed template contents are shown below:

Template:Rnd/sandbox:     (proposed version)
 * 0

After update, then Template:Rnd/a should become almost totally unused, as in what-links-here for {Rnd/a}. The table below shows the before-and-after results as identical, with every digit exactly the same after the update, but just rounded using 1 template to begin rounding, rather than 2 nested templates:

Once Template:Rnd has been updated, the table above should appear unchanged, because all of the rounded amounts will be rounded in exactly the same manner as before, with every digit exactly the same, but rounded using 1 less subtemplate, in over 307,000 calculations. -Wikid77 00:31, 5 April 2010 (UTC)
 * ✅ Looks like the change has not produced any errors  Ron h jones (Talk) 00:50, 5 April 2010 (UTC)


 * Thanks. Most of those 307,000 page reformats were finished before 5am UTC, but a suspicious 1,196 pages still claimed links to {Rnd/a}. Upon editing some of those 1,196, during edit-preview they still claimed {Rnd/a}, but after removing {Convert} & re-adding {Convert}, then they finally de-linked {Rnd/a}: I guess the database "got tired" of unlinking 306,000 {Rnd/a} and went on "de-linking strike". Hence, some articles still insist they use {Rnd/a}, when they really don't. Not a problem, with 99.97% of prior pages de-linked, we can handle the rebellious pages later. -Wikid77 Wikid77 (talk) 14:46, 5 April 2010 (UTC)

On the one hand we've cut out one layer. On the other hand we're doing the same calculation three times instead of once. Have we condensed the template or expanded it? I'm not actually sure. It's worth checking out. J IM ptalk·cont 09:28, 12 November 2010 (UTC)
 * Compare contents above in: "". I spent some hours analyzing the general issues. The new April-2010 {Rnd} uses 3 expressions, just as the prior {Rnd} with {Rnd/a}, together, had used 3 expressions of #expr. The use of "round" 3 times (to round parameter 1) is trivial, in terms of internal computing, just like trying to avoid adding "2+2" three times by invoking another template. For template-expansion space, the comparison is about equal, rather than 1 being 3x times the other 2. For editors, they no longer see "Rnd/a" in the template list during article-edits. As for overhead, Wikipedia dropped 308,000 transclusions of {Rnd/a}, allowing another template to enter the list of the top 50 most-used templates. If we could avoid nesting another subtemplate, it would have similar benefits. Meanwhile, people are using "57" varieties of "ft_to_m" because they noted that Convert was exceeding the template-nest limits. -Wikid77 (talk) 13:57, 15 November 2010 (UTC)

Bypassing the b & c (& d) subtemplates too
When the template was created we had to deal with pre-expand limits. This (in part) explains the design. Nesting several levels of small templates allowed us to achieve a very low pre-expand size whereas a tree of nested ifs might have caused strife. Improvements have since been made and we no longer have to worry about the size of stuff which doesn't get included. This has shifted the focus towards another limit we (still) have to deal with: template nesting. This is what Wikid is talking about above.

I've written a version in the sandbox which uses an elaborate if tree to bypass the level c subtemplates (,, etc.). We could bypass the b level too, this would involve repeating the round function several times but according to the above discussion it would seem to be worth it. J IM ptalk·cont 06:31, 16 February 2012 (UTC)

I've revised the sandbox version. It cuts levels b, c & d (d is only for scientific notation though). J IM ptalk·cont 14:33, 16 February 2012 (UTC)

I've just copied the new version from the sandbox. J IM ptalk·cont 03:24, 20 February 2012 (UTC)

Ifexpr error causing strife
As noted on the convert talk page is broken. These should both give "6". The first (used by rnd/b1) is not working properly (giving "4"). Luckily the second is working.

Here's the new code for (it goes beyond the page). 

Let's fix rnd/b-1 whilst were're at it (just change the  to a  ).

J IM ptalk·cont 09:21, 12 November 2010 (UTC)
 * rnd/b1 done, for rnd/b-1, do both /10^5 need to become *10^-5, or just the last one ? —Th e DJ (talk • contribs) 14:26, 12 November 2010 (UTC)


 * It seems only the last one is causing the problem ... but who knows ...

Another thing: The template is (still) misbehaving in infoboxes; this was actually the problem that lead me here in the first place. I'm hoping to try some things out which will involve a rewrite of the main page and of rnd/a. J IM ptalk·cont 14:46, 12 November 2010 (UTC)

I'd like to try the following on 

and undo the most recent change. If it doesn't fix the problem you see on the right, just revert (though it might not be immediate). If it does fix it, it'll be time to try work the said edit back in ... or perhaps not (I'm not quite convinced about it anyway). J IM ptalk·cont 15:01, 12 November 2010 (UTC)
 * Could you carry out tests on Template:Rnd/sandbox rather than the live template? &mdash; Martin (MSGJ · talk) 16:55, 12 November 2010 (UTC)
 * Yes and no. We can test the code in the sandbox and see how this template goes but we won't know whether it fixes the convert-infobox clash until it's running. J IM ptalk·cont 17:00, 12 November 2010 (UTC)
 * ... It's now in the sandbox & everything looks fine (see the table above). J IM ptalk·cont 17:07, 12 November 2010 (UTC)
 * Okay, I made the change, but it didn't fix the transclusion depth issue, so I reverted it for now. If this wasn't supposed to fix this issue, let me know and I can re-implement it.  Thanks! Plastikspork ―Œ (talk)  16:54, 13 November 2010 (UTC)
 * Whilst it was running did you see anything happen to the horrible red mess over there? J IM ptalk·cont 10:40, 14 November 2010 (UTC)
 * No, there was no change in the red mess in the infobox there floating on the right. It also didn't appear to break anything, so I can reimplement it if you want. By the way, I just added another example, to expose the nature of the infobox problem.  Basically, each call to infobox adds two to the transclusion depth.  When the infobox is within another template, that adds one as well.  So, at level 2 and 4, everything is fine, but when we jump to over 4, it breaks. Plastikspork ―Œ (talk)  17:58, 14 November 2010 (UTC)

Regarding the problem with #ifexpr: The expression (1200000.002)round(2) should be passed on as parameter, instead of its result 1.2E+6, or the result should be rounded again, because 1.2E+6 is treated as 1.2, rounded to a representable number, multiplied by 1,000,000, and in some of such cases the result is not the round number. Also, to check whether it is a multiple of 100,000, divide by 100,000, round to an integer, multiply by 100,000, and compare with the original:  →. This works correctly for numbers below 2.8e17 and non-negative second argument of round, because the multiplication is exact in these cases, in combination with the fact that the equality in #ifexpr is exact.--Patrick (talk) 00:13, 14 November 2010 (UTC)
 * (should be 1200000.00 1,200,000.00; at the time of writing, this gives the wrong 1.2E+6.00).
 * Thanks. This will be useful info in trying to get this working. J IM ptalk·cont 10:40, 14 November 2010 (UTC)
 * I made the change.--Patrick (talk) 00:45, 16 November 2010 (UTC)

Avoid errors by setting precision
Reminder: The template-nesting can be reduced by specifying precision in Convert, rather than the default which invokes the nested templates to determine precision. Notice, in the examples, below, how the errors were avoided when precision was specified as round "0" or "sigfig=5" and such.

Sometimes, it is faster to hard-code the precision for certain cases, rather than try to redesign and modify the {Precision} or {Rnd} templates and risk upsetting 310,000 other articles. If people, in general, always put "0" or "sigfig=3" (or similar), then those conversions will be faster and bypass the {Precision} templates completely.

I initiated the prior update to {Rnd} to bypass {Rnd/a} and directly invoke {Rnd/b*} because that uses 1 fewer template in the 40-deep limit. The usage figures revealed that {Rnd/a}, no longer transcluded, was dropped from 308,000 articles (after a few days). However, we need to get the developers to raise the template-nesting limit to 100 (or 60 or such). Convert is complex, but this is "not Rocket Science" and we need at least 50-deep nesting to expand Convert and other templates for validation and error-message subtemplates. You might know the 40 limit is pathetic, similar to a "call stack" limited to only 40 procedures (subroutines) nested. In modern times, call-stack depth is almost unlimited, so 100 (or 200 or 500) would be more reasonable. However, there might be some issues of "efficiency" which would limit depth to 60 as less of a resource drain. The MediaWiki software has been so peculiar, and concepts of modern software might not apply, except beware that many software developers have fragile egos caused by the strain of all the details being juggled in a nightmare of total software they didn't all write. -Wikid77 (talk) 13:23, 15 November 2010 (UTC)

A hack
I'm cheating, I know, but I think I've got rid of those error messages. Getting garbage in the second parameter was the cause. Now I tweeked the template to round to two significant figures in this case. Of course, the real solution is fixing but before we can get  in order we have to get this one working smoothly. J IM ptalk·cont 07:07, 21 February 2012 (UTC)

New templates
I've created three new rounding templates. J IM ptalk·cont 17:08, 21 February 2012 (UTC)
 * rndfrac for rounding to a fraction
 * rndnear for rounding to a multiple
 * rndgaps for formatting using gaps

Lua implementation
I created rnd/sandbox which invokes Lua to make this template much faster. The output at template:rnd/testcases seems to be identical to the current rnd, except for very long numbers where the truncation is slightly different, but I would appreciate another set of eyes to make sure. Dragons flight (talk) 21:20, 21 February 2013 (UTC)
 * Is is possible this change is the cause of the widespread errors I'm seeing with automatic population density in Infobox settlement? (See Template talk:Infobox settlement). If so, perhaps a revert is in order. —Stepheng3 (talk) 23:14, 22 February 2013 (UTC)


 * I rolled back the change, can you provide information about what case is actually breaking? Dragons flight (talk) 23:18, 22 February 2013 (UTC)
 * The relevant invocations of Rnd are buried down in the population density calculations of Infobox settlement which I believe are performed in Infobox settlement/densdisp. Articles affected included Calistoga, California.  The revert seems to have cleared up the problem, btw. —Stepheng3 (talk) 23:25, 22 February 2013 (UTC)
 * Infobox settlement/densdisp This page showed errors in all "Result" columns of the first two tables before the changes were reverted. --hydrox (talk) 23:30, 22 February 2013 (UTC)
 * I've setup a testbed using User:Stepheng3/sandbox, Template:Infobox settlement/sandbox, Template:Infobox settlement/densdisp/sandbox, and Template:Rnd/sandbox in order to recreate the problem. —Stepheng3 (talk) 23:43, 22 February 2013 (UTC)
 * Infobox settlement/densdisp needs expert attention before it will work with the new version of Rnd. Please stand by... —Stepheng3 (talk) 02:46, 23 February 2013 (UTC)

Should not be "round" not "rnd"; "rnd" is redundant duplication
•This template never should have been named "rnd", but "round". (Too terse - I have never rnded a number in my life.) ("rnd" is the random number generator function in some older programming languages.) Trying to find an excuse for {rnd}: Compactness at the expense of unclarity? Fetish/prfrnce for cprtc tmplt names?

•Template:Round existed since 2006-04-16. Template:Decimals existed since 2006-12-07. Despite this, Jimp created Template:Rnd on 2007-09-21. Trying to find an excuse for this addition: Didn't know and didn't look? Chose to skip the sandbox and red tape, to get the alternate implementation out there?

Both templates refer other templates for retaining trailing zeros. The names derived differently. → −123.00; → 123.00

{rnd} is "beating" (round} (not that that proves anything), but {decimals} is beating {rnd/-}:
 * Template:Rnd has 245,834 transclusions (and is "protected") and Template:Rnd/- has 440 (and is not "protected" at all).
 * Template:Round has only 7,022 (and is only semi-protected) and Template:Decimals has 12,804 transclusions (and is "protected").

A way out is a little harsh, but might go smoothly: 0) Compare codes, in case improvement to {rnd} is possible. 1) Replace {round} with this [compatible] code. 2) Redirct {rnd} to {round}. (Because I prefer {round}, I urge to NOT redirect the minority {round} to (rnd}.) -A876 (talk) 17:02, 20 November 2017 (UTC)

Nominated for merging: Templates for discussion/Log/2018 December 14
Please add inside . —&thinsp;JJMC89&thinsp; (T·C) 07:17, 15 December 2018 (UTC)
 * ✅ ~ Amory  (u • t • c) 10:40, 15 December 2018 (UTC)

Protected edit request on 15 December 2018
Please replace the contents of this template with the contents of the sandbox in order to remove an undesirable line break that is causing Linter missing end tag / stripped tag errors. See User:Jonesey95/sandbox3 for an example. Thanks. – Jonesey95 (talk) 14:23, 15 December 2018 (UTC)
 * Pinging, if you are around. Thanks. – Jonesey95 (talk) 14:24, 15 December 2018 (UTC)
 * Yes check.svg Done Yeah, that's on me, window size hid the newline. Thanks. ~  Amory  (u • t • c) 15:15, 15 December 2018 (UTC)

Merge with Template:decimals broke Template:Physical constants
See the auto-rounded version of DnuCs in the table at Template:Physical constants/doc:. Basically, the output of was being fed to, and for that constant, the value is 10 digits long (9192631770), and the resultant exponential notation  was not valid input to.

I fixed it by changing to use   rather than use a more complex pretty-printer like  at all. But since you're doing the conversion and are familiar with the whole thing, you might want to take a look. 167.88.12.231 (talk) 02:54, 2 June 2019 (UTC)
 * I could have sworn I had looked at all of the templates calling Template:Decimals and checked for any cases like this before doing the redirection after the merge was reverted two months ago because Template:Inflation broke in exactly the same way. I probably missed this one because of the large amount of markup between the decimals and the rounding template. Note to self: Merging templates is harder than you think ... * Pppery * it has begun... 03:08, 2 June 2019 (UTC)

Error cat
Can Category:Pages with bad rounding precision show only article-space? , another one. Thanks. MB 02:56, 19 November 2019 (UTC)
 * ✅. – Jonesey95 (talk) 17:49, 19 November 2019 (UTC)

Bug in rounding 0.000020004?
In Examples:

gives 000 for some reason (template: )

Shouldn't this be at least 0.00002, or exactly 0.0000200?

Tests:

->

->

->

->

-> MarMi wiki (talk) 17:21, 24 April 2021 (UTC)
 * The code at precision_format in Module:Math would need serious time to investigate. I don't know why lang:formatNum is used there (for localization of digits? in only that function?) but it is the cause. When the value is 0.000020004, line 478 gives the string " ". Johnuniq (talk) 03:57, 25 April 2021 (UTC)
 * It seems that something is wrong with the lang:formatNum in some languages (en, de, ...).
 * From debug console at Module:Math:
 * MarMi wiki (talk) 22:22, 25 April 2021 (UTC)
 * MarMi wiki (talk) 22:22, 25 April 2021 (UTC)
 * MarMi wiki (talk) 22:22, 25 April 2021 (UTC)
 * MarMi wiki (talk) 22:22, 25 April 2021 (UTC)

You have edited Template:Round or Module:Math and are active. You might not have been involved with the code for round (which uses ) but you might have knowledge of how this template is used? I'm too irritated by "what links here" to work out the significant usages of. Surely it cannot be cases like that reported here where fixed text is entered into the template because it would be easier to simply write the rounded value. Instead, presumably the value to be rounded comes from another template or a calculation. At any rate, lang:formatNum is incompatible with round because formatNum requires a number as input and converting what round is doing to a number will lose round's work. Also, formatNum will format the output text according to its own rules. I guess it's intended use was to insert commas but the vagaries of floating point representations mean that working with a number (as opposed to a string) will always fail occassionally. Any thoughts? Where did you notice the problem or were you just experimenting? Johnuniq (talk) 11:05, 26 April 2021 (UTC)
 * My guess is that the problem happens in the code starting at line 462:

-- Due to round-off effects it is neccesary to limit the returned precision under -- some circumstances because the terminal digits will be inaccurately reported. if order + precision >= 14 then if order + p._precision(value_string) >= 14 then precision = 13 - order; end end
 * These test cases appear to have order+precision greater than 14, so they trigger this apparently buggy workaround. Just guessing. I would bring it up at Module talk:Math. – Jonesey95 (talk) 16:16, 26 April 2021 (UTC)
 * @Johnuniq:
 * I've noticed this looking at the template documentation - it's one of the examples (second one). I've added it also as a testcase in the template test page.
 * Lang:formatNum in some languages (en included, pl excluded) will return 0 at any small number starting with 4 zeroes (ex. 0.00001), so I think that's this function problem (or it's a locale problem).
 * I've reported this on Mediawiki Extension talk:Scribunto: "lang:formatNum returns 0 in some languages for small numbers (starting with 4 zeroes)". MarMi wiki (talk) 18:31, 26 April 2021 (UTC)
 * Added 0.00001 & 0.0001 in Template:Round/testcases. Template should prevent returning multiple zeroes (00+), since  doesn't add any. - I forgot that the template invokes Module:Math.
 * Reported on the Module:Math talk page, with possible solution: lang:formatNum_fix_for_small_numbers_of_order -5 and_smaller. MarMi wiki (talk) 18:45, 26 April 2021 (UTC)

Math expressions
Why is it not mentioned anywhere in the documentation that  supports mathematical expressions?



I have learned about the existence of this functionality from the source code of. — UnladenSwallow (talk) 23:13, 11 July 2021 (UTC)