DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Format Function

08.28.2009
| 3161 views |
  • submit to reddit
        // The Format function formats a number, date or time based on the user entered format type.
// There are two required statements: expression and format. 
// Expression represents the number, date or time you are trying to format. 
// The format argument tells the system how to display the input expression.
// Acceptable values for the format argument are: Short Time, Long Time, Short 
// Date, Long Date, General Date, General Number, Currency, Fixed, Standard, 
// Percent, Yes/No, True/False, or On/Off.
//
// Usage: string = Format(expression, format)

'--- declare and set variables
dim num, datetime
num = 1741534.542058484
datetime = Now()

'--- Short Time:
Response.Write Format( datetime, "Short Time" ) 
'--- Long Time:
Response.Write Format( datetime, "Long Time" )
'--- Short Date:
Response.Write Format( datetime, "Short Date" )
'--- Long Date:
Response.Write Format( datetime, "Long Date" )
'--- General Date:
Response.Write Format( datetime, "General Date" )
'--- General Number:
Response.Write Format( num, "General Number" )
'--- Currency:
Response.Write Format( num, "Currency" )
'--- Fixed:
Response.Write Format( num, "Fixed" )
'--- Standard:
Response.Write Format( num, "Standard" )
'--- Percent:
Response.Write Format( num, "Percent" )
'--- Yes/No:
Response.Write Format( num, "Yes/No" )
'--- True/False:
Response.Write Format( num, "True/False" )
'--- On/Off:
Response.Write Format( num, "On/Off" )

Private Function Format(byVal expression, byVal strFormat)
    On Error Resume Next
    Select Case lcase( strFormat )
        Case "general date"
            Format = FormatDateTime(expression, 0)
        Case "long date"
            Format = FormatDateTime(expression, 1)
        Case "short date"
            Format = FormatDateTime(expression, 2)
        Case "long time"
            Format = FormatDateTime(expression, 3)
        Case "short time"
            Format = FormatDateTime(expression, 4)
        Case "general number"
            Format = Replace( expression, ",", "" )
        Case "currency"
            Format = FormatCurrency(expression, 2)
        Case "fixed"
            Format = Replace(FormatNumber(expression, 2, -1), ",", "")
        Case "standard"
            Format = FormatNumber(expression, 2, -1)
        Case "percent"
            Format = FormatPercent(expression, 2)
        Case "yes/no"
            expression = cLng(expression)
            If expression = 0 then 
                Format = "No" 
            else 
                Format = "Yes"
            end if
        Case "true/false"
            expression = cLng(expression)
            If expression = 0 then 
                Format = "False" 
            else 
                Format = "True"
            end if
        Case "on/off"
            expression = cLng(expression)
            If expression = 0 then 
                Format = "Off" 
            else 
                Format = "On"
            end if
        Case Else
            Format = expression
    End Select
    On Error GoTo 0
End Function