Baseconvert

用來把數字轉換成2到36之間的任何進位制,可用於}、}、}等模板當中。

文档图示 模块文档

使用方法

local BaseConvert = require('Module:BaseConvert') BaseConvert.convert({n = 14600926, base = 16}) -- returns 'DECADE' 

參數:

  • n:(必填)須轉換的數字,以字符串形式輸入。十进制的數字可直接以數字形式輸入。
  • base:(必填)目標進位制的底數,可取2至36(含)間的任何數字,不接受不是整數的底數,如进制(若底數輸入非整數或範圍外[2,36]的數將輸出不正確的結果)。若需要轉換不是整數的底數可以改用Module:BigNumber提供的convertBase函數
  • from:輸入值的進位制的底數,默認為10(如果輸入值以「0x」開頭,則默認為16),不接受不是整數的底數。所有非十进制都不支持分數,若需要計算非十进制的分數可以改用Module:BigNumber提供的convertBase函數
  • precision:小數點後的位數,達不到時會加「0」。不填該項會顯示所有位數,但不超過10位數。
  • width:小數點前至少顯示的位數,達不到時會加「0」。
  • default:n為空或非數字時所輸出的值,默認為n的值。
  • prefix:加在輸出值前的維基代碼。n為空或非數字時則不加。例如在轉換到十六进制後在前面加上0x
  • suffix:加在輸出值後的維基代碼。n為空或非數字時則不加。例如在轉換到八进制後在後面加上8

請注意,若輸入的數字過大,絕對值超過9007199254740991的話,可能會導致運算精度丟失。若需要轉換大數的进制可以改用Module:BigNumber提供的convertBase函數

從模板调用

從模板调用時,這個模組支援以ntom(n进制轉m进制)的函數名稱進行调用,例如:

代码 效果
{{#invoke:BaseConvert|16to10|  FF  }}

255

{{#invoke:BaseConvert|10to36|500}}

DW

{{#invoke:BaseConvert|10to16|Foo|default=0}}

0

這種调用方式除了|base=|from=|n=外的所有上列的參數皆可使用。也可以调用convert達到相同效果。

例如:

參見

local p = {}  local digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'  local function normalizeFullWidthChars(s) return mw.ustring.gsub(s, '[!-~]', function(s) return mw.ustring.char(mw.ustring.codepoint(s, 1) - 0xFEE0) end) end  local function _convert(n, base, from, precision, width, default, prefix, suffix) n = tostring(n)  -- strip off any leading '0x' (unless x is a valid digit in the input base) from = tonumber(from) if not from or from < 34 then local c n, c = n:gsub('^(-?)0[Xx]', '%1') if c > 0 and not from then from = 16 end end  -- check for a negative sign. Do this while the input is still in string form, -- because tonumber doesn't support negative numbers in non-10 bases. local sign = '' local c n, c = n:gsub('^-', '') if c > 0 then sign = '-' end  -- replace any full-width Unicode characters in the string with their ASCII equivalents n = normalizeFullWidthChars(n)  -- handle scientific notation with whitespace around the 'e' e.g. '5 e7' n = n:gsub('%s*[eE]%s*', 'e')  from = from or 10 local num = tonumber(n, from) base = tonumber(base) precision = tonumber(precision) width = tonumber(width)  if not num or not base then return default or n end  local i, f = math.modf(num)  local t = {} repeat local d = (i % base) + 1 i = math.floor(i / base) table.insert(t, 1, digits:sub(d, d)) until i == 0 while #t < (width or 0) do table.insert(t, 1, '0') end local intPart = table.concat(t, '')  -- compute the fractional part local tf = {} while f > 0 and #tf < (precision or 10) do f = f * base i, f = math.modf(f) table.insert(tf, digits:sub(i + 1, i + 1)) end  -- add trailing zeros if needed if precision and #tf < precision then for i = 1, precision - #tf do table.insert(tf, '0') end end  local fracPart = table.concat(tf, '')  -- remove trailing zeros if not needed if not precision then fracPart = fracPart:gsub('0*$', '') end  -- add the radix point if needed if #fracPart > 0 then fracPart = '.' .. fracPart end  return (prefix or '') .. sign .. intPart .. fracPart .. (suffix or '') end  function p.convert(frame) -- Allow for invocation via #invoke or directly from another module local args if frame == mw.getCurrentFrame() then args = frame.args else args = frame end  local n = args.n local base = args.base local from = args.from local precision = args.precision local width = args.width local default = args.default local prefix = args.prefix local suffix = args.suffix return _convert(n, base, from, precision, width, default, prefix, suffix) end  setmetatable(p, { __index = function(t, k) local from, base = k:match('^([0-9]+)to([0-9]+)$') if not from then return nil end return function(frame) local args = frame.args return _convert(mw.text.trim(args[1]), base, from, args.precision, args.width, args.default, args.prefix, args.suffix) end end })  return p 

Tags:

Template:BinaryTemplate:HexadecimalTemplate:Octal

🔥 Trending searches on Wiki 中文:

元朝華麗計程車行意大利從Lv2開始開外掛的前勇者候補過著悠哉異世界生活我獨自升級李嘉誠邱宇辰四通桥事件毛里求斯刘少奇Re:Monster全球变暖郭富城大力女子姜南順习明泽可憐的東西 (電影)劉德華国际劳动节梁思浩科比·布莱恩特各国家和地区人口列表慈禧太后排球少年!!中华人民共和国河北彩花EXO上海市告五人王鶴棣只要有空KISS OF LIFE (組合)繼承者們張藝興鄂靖文李克强張雷 (香港)千与千寻林夏薇蔡英文古天樂無職轉生~到了異世界就拿出真本事~角色列表神舟十八号淚之女王奥斯卡最佳女配角奖坦克人戚薇TP-Link函館市武则天變裝皇后蔡清祥FIFTY FIFTY李到晛Oh!三光公寓!何赛飞2024年NBA季後賽林彪中越战争奥斯曼帝国賀軍翔各國死刑列表陳明義 (1967年)伊斯兰教新世纪福音战士安俞真印度尼西亚富都青年旅行者1号四海 (电影)三年困难时期翡翠台電視劇集列表 (2020年代)Pornhub徐睿知光荣之路 (2006年电影)柯玲玲🡆 More