Module:Utilities

Revision as of 03:46, 17 September 2024 by Sur (talk | contribs) (initial for round_to_sig_fig)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Overview

Utility functions that serve any miscellaneous purpose

Functions

get_slot_color

Retrieve's the color of a certain slot/category in any color format (default hex), formerly known in english as Weapon, Vitality, and Spirit, though their unlocalized names are Weapon, Armor, and Tech. Utilized by Template:SlotColor

Parameters

  • slot - slot key, options are Weapon, Armor, and Tech
  • color_format - color format - Defaults to hex. Valid options are hex, rgb, hsl, cmyk
  • no_wrap - (OPTIONAL) - Defaults to false. Set to true to remove prefixes and postfixes of '#' from hex, or 'rgb()', 'hsl()', and 'cmyk()' from the other formats. May be useful for altering the values dynamically
  • debug_mode - (OPTIONAL) - Defaults to false. Used mostly for documentation purposes

Examples

See examples at Template:SlotColor


local p = {};

	--round_to_significant_figures(12345.6789, 3)  -- Output: 12300
	--round_to_significant_figures(0.0012345, 2)   -- Output: 0.0012
	--round_to_significant_figures(-98765, 4)      -- Output: -98760
	function p.round_to_sig_fig(num, n)
	    if num == 0 then
	        return 0
	    end
	
	    -- Calculate the order of magnitude (log10 returns the logarithm base 10)
	    local order_of_magnitude = math.floor(math.log10(math.abs(num)))
	    
	    -- Scale the number to move the significant digits into the integer part
	    local scale = math.pow(10, n - 1 - order_of_magnitude)
	    
	    -- Round the scaled number and then scale it back
	    local rounded_num = math.floor(num * scale + 0.5) / scale
	    
	    return rounded_num
	end

return p