Module talk:Formatnum

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

Error[edit]

@Jarekt and Verdy p:

local FN = require "Module:Formatnum"
function p.main (frame)
	return FN.formatNum(123.45, 'ca', 1)..' '..FN.formatNum(123.49, 'ca', 1)..' '..FN.formatNum(123, 'ca', 1)
end

In the above example, it returns:

123,4 123,4 123.0

Where the last value (123.0) is incorrect. It must be 123,0, with comma separator.

Jmarchn (talk) 14:48, 14 August 2020 (UTC)[reply]

I can reproduce the issue since {{formatnum|1=123|2=ca|prec=1}} gives "123,0". I will look into it. --Jarekt (talk) 16:59, 14 August 2020 (UTC)[reply]
@Jarekt: OK. But this not solve the error. Other example {{formatnum|1=123|2=ar|prec=1}} returns "١٢٣٫٠". This is incorrect. The correct is "١٢٣٫٠". A point (".") is used incorrectly as decimal separator when the number is an integer (then converted to real number), instead of a comma (",") in Catalan, French, Spanish, etc., or the character ("٫") in Arabic.
The error is in the line 73 where:
dot = '.' -- must be added
I find a solution:
dot = mw.ustring.sub (mw.language.new(lang):formatNum(0.1), 2, 2) -- must be added
Jmarchn (talk) 21:31, 15 August 2020 (UTC)[reply]
That was a good solution.and it is deployed now. --Jarekt (talk) 03:05, 16 August 2020 (UTC)[reply]
I have a better solution in the sandbox, which also correctly fixes the correct rounding when there are more decimals than the requested precision.
That way it was done does not work properly because it alters the number formatted with basic English, before handling localisation of the number. And rounding must be comptued first in the Basic English format, before we handle the locale-sensitive digits, grouping separators (at variable positions), and decimal separator.
The sandbox version does all the rounding in English, adding the padding precision digits, before converting to the locale's digits and separators (but keeping all decimal digits as they are after the rounidng and padding of zeroes). It also corectly check the maximum precision supported by the 64-bit floattting point numbers in the local Lua implementation. I've commented a bit more the critical parts of the conversion so that all is done in the correct order. Rounding is more complex than what I thought initially, but it now works, and it's still using number:tostring() to do the basic decimal conversion. It now allows formatting to all decimal numeric systems existing in Unicode (using special "wildcard language codes" that are now part of BCP47, such as "*-Latn" to match any language for a given script or numeral system. verdy_p (talk) 19:09, 17 September 2021 (UTC)[reply]

Function required[edit]

Hi Jarek, because I need a number formatting function, and I think it would fit well into Formatnum (not useful to create an own module), and I think also that it can be used generally by others: how do you think about taking that function into that module?

-- strip removes from a string(1) all characters specified by a pattern(2). 
-- default for pattern is the space; pattern can be e.g. "[.,-%s]". 
-- e.g {{#invoke:..|strip|12,3334.556666 $|[$€.,-%s]}} will remove ",. $"
p.strip = function(frame)
	return mw.ustring.gsub( frame.args[1] or " ", frame.args[2] or " ", "" ),_
end --  function: remove from string (by pattern) 

-- sarang사랑 12:47, 12 January 2021 (UTC)[reply]

sarang, before I add it can you check if Module:String#replace can be used instead, those 2 functions seem to have the same purpose. --Jarekt (talk) 19:27, 12 January 2021 (UTC)[reply]