Jump to content

Topic on Extension talk:Scribunto

lang:formatNum returns 0 in some languages for small numbers (starting with 4 zeroes)

4
MarMi wiki (talkcontribs)

I don't know if this is the right place, but:

In some languages (en, de, ...) formatNum returns 0 instead of the formatted number when the number starts with 4 zeros (ex. 0.00001). But when called with noCommafy option, the number is formatted just fine (into exponential notation). From the lua module dev console:

mw.log(mw.language.new( 'en' ):formatNum( 0.000020004))

0

mw.log(mw.language.new( 'en' ):formatNum( 0.000020004, { noCommafy = true } ))

2.0004E−5

For some other languages (at least for pl) the formatted number is returned for both cases.

Context: :en:Template_talk:Round#Bug_in_rounding_0.000020004?.

FeRDNYC (talkcontribs)

The problem seems to have everything to do with the output of scientific-notation values in locales that use . separators; same issue if the input is expressed in scientific notation:

mw.log(mw.language.new( 'es' ):formatNum( 9.04E-8 ))
9,004E−8
mw.log(mw.language.new( 'en' ):formatNum( 9.004E-8 ))
0
mw.log(mw.language.new( 'zh_CN' ):formatNum( 9.004E-8 ))
0

I also feel like the commafy feature (when not turned off) is breaking the output of languages that use comma decimal separators, as well. Like, why are these different? (I'm not even sure which one is correct, TBH, but I feel like they shouldn't be different.)

mw.log(mw.language.new( 'es' ):formatNum( 1.0E20, {noCommafy = true} ))
1.0E+20
mw.log(mw.language.new( 'es' ):formatNum( 1.0E20 ))
1,0E+20

And then there's this:

mw.log(mw.language.new( 'en' ):formatNum( 1.0E20 ))
100,000,000,000,000,000,000
mw.log(mw.language.new( 'en' ):formatNum( 1.0E20, {noCommafy = true} ))
1.0E+20
MarMi wiki (talkcontribs)

In general, this could be a fault in regional settings, not in Scribunto.


About noCommafy (second example) - both are correct. First (with noCommafy) prints number in the format used internally by wikipedia (dot separator), the second one (without noCommafy) apply formatnum:.


Last example - I noticed that too, but it's not that big of a deal (the number is still parseable) - although it can suggest that the number is "exact", when sometimes it isn't (because it exceeded the precision):

mw.log(mw.language.new( 'en' ):formatNum( 1999999999999999))
1,999,999,999,999,999

mw.log(mw.language.new( 'en' ):formatNum( 19999999999999990))
20,000,000,000,000,000

mw.log(mw.language.new( 'en' ):formatNum( 1999999999999999, {noCommafy=1} ))
1999999999999999

mw.log(mw.language.new( 'en' ):formatNum( 19999999999999990, {noCommafy=1} ))
2.0E+16

FeRDNYC (talkcontribs)

Yeah, as I read up a bit more on :formatNum and the original {{formatnum}} parser function that preceded it, I realized that noCommafy was equivalent to the old |NOSEP parameter to {{formatnum}}, which exhibits the same behavior.

It's a bit disconcerting, though, I'll admit. I would've expected |NOSEP (and thus noCommafy) to turn off thousands-separator insertion, but not localization of the decimal mark. IOW, to be the difference between...

  • English: 1,000,000.123 and 1000000.123
  • Spanish: 1 000 000,123 and 1000000,123

But that's not how it works a'tall. #NowIKnow

Reply to "lang:formatNum returns 0 in some languages for small numbers (starting with 4 zeroes)"