משתמש:Beyond doubt/יחידה:המרת מספרים ואותיות

מתוך ויקיטקסט, מאגר הטקסטים החופשי

local z = {}

-- Convert number to hebrew letters function z._num_to_he( first )

   result = ""
   --return first

   if first then -- if NaN or nil, will skip down to final return
       while first >= 400 do
           result = result .. "ת"
           first = first - 400
       end
       if first >= 300 then
           result = result .. "ש"
           first = first - 300
       elseif first >= 200 then
           result = result .. "ר"
           first = first - 200
       elseif first >= 100 then
           result = result .. "ק"
           first = first - 100
       end
       if first == 15 then return result .. "טו" end
       if first == 16 then return result .. "טז" end
       if first >= 90 then
           result = result .. "צ"
           first = first - 90
       elseif first >= 80 then
           result = result .. "פ"
           first = first - 80
       elseif first >= 70 then
           result = result .. "ע"
           first = first - 70
       elseif first >= 60 then
           result = result .. "ס"
           first = first - 60
       elseif first >= 50 then
           result = result .. "נ"
           first = first - 50
       elseif first >= 40 then
           result = result .. "מ"
           first = first - 40
       elseif first >= 30 then
           result = result .. "ל"
           first = first - 30
       elseif first >= 20 then
           result = result .. "כ"
           first = first - 20
       elseif first >= 10 then
           result = result .. "י"
           first = first - 10
       end
       if first >= 9 then
           result = result .. "ט"
       elseif first >= 8 then
           result = result .. "ח"
       elseif first >= 7 then
           result = result .. "ז"
       elseif first >= 6 then
           result = result .. "ו"
       elseif first >= 5 then
           result = result .. "ה"
       elseif first >= 4 then
           result = result .. "ד"
       elseif first >= 3 then
           result = result .. "ג"
       elseif first >= 2 then
           result = result .. "ב"
       elseif first >= 1 then
           result = result .. "א"
       end
   end
   
   return result

end

function z.num_to_he( frame )

   local args = frame.args;
   if args[1] == nil then
       local parent = frame:getParent();
       args = parent.args;
   end
   first = tonumber(args[1])
   quotes = args[2]
   q = false
   if quotes == "מרכאות" then
       sinquote = "'"
       dblquote = '"'
       q = true
   elseif quotes == "גרשיים" then
       sinquote = "׳"
       dblquote = '״'
       q = true
   end
   result = ""
   if first > 1000 then
       local thousands = (first-first%1000)/1000
       result = z._num_to_he(thousands)
       if not q then
           result = result .. "'"
       else
           result = result .. sinquote
       end
       first=first%1000
   end
   result = result .. z._num_to_he(first)
   if not q then
       return result
   elseif mw.ustring.len(result) == 1 then
       return result .. sinquote
   else
       return mw.ustring.sub(result,1,-2) .. dblquote .. mw.ustring.sub(result,-1)
   end

end

return z