Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Format Number Function

    Hey all,

    I need to have a function that returns the value of a string (which is made of all numbers) in a 000.00 format. For example, if the str is "48" then I need it to return 048.00. My current function doesnt really do this as I am just using FormatNumber()

    Function Meas(str)
    if str <> "" then response.write FormatNumber(str,2)
    End Function

    How can I go about easily formatting this number without writting a buncha If/Then statements?

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
            
    Function PadZero(str,toLen)
        If Len(str) < toLen Then
           PadZero = String(toLen-Len(str),"0") & str
        Else
           PadZero = str
        End If   
    End Function
    
    
    num = 48
    
    call it like this:
    x = PadZero(FormatNumber(num,3),7)
    BaldEagle

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For the purpose of encapsulation I thought I would through another version at you. You may have already decided to do something like this.

    Code:
    Function PadString(str,whatChar,whichDirection,toLen)
        If Len(str) < toLen Then
           If whichDirection Then
              'pad LEFT if whichDirection is true
              PadString = String(toLen-Len(str),whatChar) & str
           Else
              ' pad RIGHT if whichDirection is false
              PadString = str & String(toLen-Len(str),whatChar)
           End If
        Else
           PadString = str
        End If   
    End Function
    
    num = 48
    
    call it like this:
    x = PadString(FormatNumber(num,2),48,true,7)
    Now it can be used to pad any string with any character in either direction. Also note that the String function will accept asci values. So passing 48 will result in the padding of "0", 42 would be "*" and so forth.

    Hope this is what you are looking for.

    BaldEagle

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    478
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Or for a "quick and dirty" way of padding without a function:

    To pad with leading 0's up to 3 digits:
    varFld = Right("000" & varFld, 3)
    J. Paul Schmidt
    www.Bullschmidt.com - Freelance Web and Database Developer
    www.Bullschmidt.com/DevTip.asp - Classic ASP Design Tips


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •