...

View Full Version : Format Number Function



DigitalFusion
03-02-2006, 02:21 AM
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?

BaldEagle
03-02-2006, 04:25 AM
Try this:



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

BaldEagle
03-03-2006, 07:44 PM
For the purpose of encapsulation I thought I would through another version at you. You may have already decided to do something like this.


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

Bullschmidt
03-07-2006, 03:16 AM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum