Bước tới nội dung

Mô đun:Inflation

Bách khoa toàn thư mở Wikipedia
local p = {}local data = mw.loadData('Mô đun:Inflation/data')function p.main(frame)	-- Dùng getArgs để xử lý tham số từ bản mẫu chuẩn hơn	local args = require('Module:Arguments').getArgs(frame)		-- Lấy các tham số đầu vào	local index = args[1] or args.index or "US"	local value = tonumber(args[2] or args.value)	local startYear = tonumber(args[3] or args.start_year)	local endYear = tonumber(args[4] or args.end_year) or tonumber(os.date("%Y"))	local round = tonumber(args.r or args.round or 0)	local fmt = args.fmt -- Tham số định dạng (c hoặc eq)	local cursign = args.cursign or "$" -- Ký hiệu tiền tệ, mặc định là $		-- Kiểm tra lỗi thiếu tham số	if not value or not startYear then 		return '<span class="error">Lỗi: Thiếu tham số giá trị hoặc năm bắt đầu</span>' 	end		-- Lấy bộ dữ liệu	local dataset = data[index]	if not dataset then 		return '<span class="error">Lỗi: Mã Index "' .. index .. '" không tồn tại trong dữ liệu</span>' 	end		local cpiStart = dataset[tostring(startYear)]	local cpiEnd = dataset[tostring(endYear)]		-- Nếu không có CPI năm kết thúc, thử lấy dữ liệu mới nhất (latest)	if not cpiEnd then		cpiEnd = dataset.latest		-- Cập nhật lại endYear thành năm mới nhất có dữ liệu thực tế để hiển thị cho đúng		if dataset.latest_year then			endYear = dataset.latest_year		end	end		if not cpiStart then 		return '<span class="error">Lỗi: Không có dữ liệu CPI năm '..startYear..'</span>' 	end		-- Tính toán lạm phát	local result = value * (cpiEnd / cpiStart)		-- Làm tròn số	local mult = 10^round	result = math.floor(result * mult + 0.5) / mult		-- Xử lý định dạng hiển thị (Format)	local resultStr = tostring(result)		-- Nếu dùng fmt=c hoặc fmt=eq, định dạng số theo chuẩn wiki (thêm dấu chấm/phẩy ngăn cách)	if fmt == 'c' or fmt == 'eq' then		resultStr = mw.getContentLanguage():formatNum(result)	end		-- Nếu dùng fmt=eq, trả về câu đầy đủ	if fmt == 'eq' then		return "tương đương " .. cursign .. resultStr .. " vào năm " .. endYear	end		-- Mặc định chỉ trả về số	return resultStrendreturn p