Firetron’s ZeroPad

1 108 Views No Comments 1 month ago
  • Pads a number with zero to have the specified number of digits like 04.200

    • This topic was modified 1 month ago by Firetron. Reason: Fixed indentation
    • This topic was modified 1 month ago by Firetron. Reason: Hid decimal when no decimal places
    HaasScript Code
    --  ============================================================================
    --    Firetron's ZeroPad
    --
    --    Pads a number with zero to have the specified number of digits like 04.200
    --
    --    Discord:  @FiretronP75
    --    BTC Tips: 14WLDZsrhshcP6qvBarKW8ABACv3iUXEro
    --    ETH Tips: 0xfaaac99fbf11f8f4abc4b3bf079cb2c7a43b5f87
    --  ============================================================================
    
    --  ========================================================
    --    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)
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free