Firetron's ZeroPad

stable
By Firetron in Miscellaneous Published August 2020 👁 1,314 views 💬 0 comments

Description

Pads a number with zero to have the specified number of digits like 04.200
HaasScript
--  ============================================================================
--    Firetron's ZeroPad
--
--    Pads a number with zero to have the specified number of digits like 04.200
--
--    Discord:  @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

local string = ''

--  ------------------------------------
--    Definition
--  ------------------------------------

local description
local output

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pAfter
local pBefore
local pNumber

--  ========================================================
--    Command Definition
--  ========================================================

description = 'Pads a number with zero to have the specified number of digits like 04.200'
DefineCommand('ZeroPad', description)

--  ========================================================
--    Parameter Definitions
--  ========================================================

description = 'The number to format.'
pNumber     = DefineParameter(NumberType, 'input', description, true, 4.2)

description = 'The number of digits before the decimal. Use 0 to not pad.'
pBefore     = DefineParameter(NumberType, 'before', description, true, 2)

description = 'The number of digits after the decimal. Use 0 to not pad.'
pAfter      = DefineParameter(NumberType, 'after', description, true, 3)

--  ========================================================
--    Functions
--  ========================================================

local ZeroPad = function (numberString, digits, isBefore)

  local length = string.len(numberString)

  local difference = digits - length

  if difference < 1 then return numberString end

  local padding = string.rep('0', difference)

  return IfElse(isBefore, padding..numberString, numberString..padding)

end

--  ----------------

local ZeroPadBoth = function ()

  local numberString = Parse(pNumber, StringType)

  local splitString = StringSplit(numberString, '.')

  local beforeDecimal = splitString[1]

  local paddedBefore = ZeroPad(beforeDecimal, pBefore, true)

  local afterDecimal = ''

  if splitString[2] != nil then

    afterDecimal = splitString[2]

  end

  local paddedAfter = ZeroPad(afterDecimal, pAfter, false)

  local length = string.len(paddedAfter)

  return IfElse(length > 0, paddedBefore..'.'..paddedAfter, paddedBefore)

end

--  ========================================================
--    Execution
--  ========================================================

output = ZeroPadBoth()

--  ========================================================
--    Output Definition
--  ========================================================

description = 'The number converted to a string with the specified digits.'
DefineOutput(StringType, output, description)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!