update
This commit is contained in:
parent
799642563f
commit
64782628fc
2 changed files with 36 additions and 0 deletions
20
helpers.rb
Normal file
20
helpers.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
def thousands(n, sep = ',')
|
||||||
|
s = n.to_s
|
||||||
|
sign = s[0] == '-' ? '-' : ''
|
||||||
|
s = s[1..-1] if sign == '-'
|
||||||
|
|
||||||
|
int, frac = s.split('.', 2)
|
||||||
|
|
||||||
|
out = ''
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
int.reverse.each_char do |c|
|
||||||
|
out << sep if count > 0 && count % 3 == 0
|
||||||
|
out << c
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
int = out.reverse
|
||||||
|
|
||||||
|
frac ? "#{sign}#{int}.#{frac}" : "#{sign}#{int}"
|
||||||
|
end
|
||||||
16
lua_init.lua
Normal file
16
lua_init.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
local function escape_pattern(s)
|
||||||
|
return (s:gsub("(%W)", "%%%1"))
|
||||||
|
end
|
||||||
|
|
||||||
|
function Thousands(n, sep)
|
||||||
|
sep = sep or "."
|
||||||
|
local s = tostring(n)
|
||||||
|
local sign, int, frac = s:match("^([%-]?)(%d+)(%.?%d*)$")
|
||||||
|
|
||||||
|
int = int:reverse():gsub("(%d%d%d)", "%1" .. sep):reverse()
|
||||||
|
|
||||||
|
local sep_esc = escape_pattern(sep)
|
||||||
|
int = int:gsub("^" .. sep_esc, "")
|
||||||
|
|
||||||
|
return sign .. int .. frac
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue