Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Format Number Function

1. ## 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?

• 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

• 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

• 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)

•

#### Posting Permissions

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