VB icon

ASP Format Money

Email
Submitted on: 1/6/2015 5:57:00 PM
By: Brian Reeves (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 2961
 
     This function properly formats a number to viewable currency ($x,xxx.xx) to be displayed on a webpage (or saved internally). Also performs rounding based on the constant. Will round up, down, and normal (5-9 = +1; else drop digits)
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :ASP Format Money
'**************************************
Open Source
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: ASP Format Money
' Description:This function properly formats a number to viewable currency ($x,xxx.xx) to be displayed on a webpage (or saved internally). Also performs rounding based on the constant. Will round up, down, and normal (5-9 = +1; else drop digits)
' By: Brian Reeves (from psc cd)
'
' Inputs:Number (to be converted into money)
'
' Returns:String representing the money in the format of
$xx,xxx.xx (where x is a number)
'
' Side Effects:Takes into account user errors (entering in < 2 digits after period).
'**************************************

'Returns a string from a number to be displayed in a $xx.xx format
Public Function FormatMoney(sString)
	'change cRound to Normal, Up, Down
	Const cRound = "Normal"
	
	If InStr(sString, ".") Then
		'Adding extra zero's at the end for error correction (User passes "23." and it
		'	will still display correctly) 
		FormatMoney = sString & "000"
		Select Case cRound
			Case "Normal"
				If Mid(FormatMoney, InStr(FormatMoney, ".") + 3) > 4 Then
					FormatMoney = Left(FormatMoney, InStr(FormatMoney, ".") + 1) & Fix(Mid(FormatMoney, InStr(FormatMoney, ".") + 2, 1) + 1)
				End If
			Case "Up"
				If Mid(FormatMoney, InStr(FormatMoney, ".") + 3) > 0 Then
					FormatMoney = Left(FormatMoney, InStr(FormatMoney, ".") + 1) & Fix(Mid(FormatMoney, InStr(FormatMoney, ".") + 2, 1) + 1)
				End If
		End Select
		FormatMoney = Left(FormatMoney, InStr(FormatMoney, ".") + 2)
	Else: FormatMoney = sString & ".00" 'Appending cents to the dollar
	End If
	
	FormatMoney = "$" & FormatMoney	'Appending dollar sign to beginning
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.