Important alert: (current site time 7/15/2013 11:25:33 PM EDT)
 

VB icon

ASP Format Money

Email
Submitted on: 3/27/2003 3:14:46 PM
By: Brian Reeves 
Level: Intermediate
User Rating: By 2 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 22100
(About the author)
 
     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!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' 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
'
' 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).
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8277&lngWId=4'for details.'**************************************

'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 5 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
4/1/2003 3:40:51 AMDara SARNIRAND

This can be done natively by ASP.
By setting "Session.LCID" to correct language code (for example 1036 for French), a simple Response.Write(CCur(mAmount)) gives "23,12 €". Set to English American, this gives "$ 23.12".
(If this comment was disrespectful, please report it.)

 
4/19/2003 2:59:25 AMEdward Welch

Dim Money
Money = "$" & FormatNumber("123.231",2)

Money would return = $123.23

Cheers
(If this comment was disrespectful, please report it.)

 
8/16/2003 2:49:01 AM

What about a way to clean up entered information. I have had several people mess up and not delete the decimal point and they they type in a dollar and cent amount. Thus you and up with something like 12.34.00

This can be passed to the datebase but when I display again or try to sent it through the format again it really has trouble.
(If this comment was disrespectful, please report it.)

 
8/16/2003 3:21:59 AM

function FormatMoney(sString)
dmarker1=0
dmarker2=0

If InStr(sString, ".") Then
dmarker1=0
dmarker2=0
dmarker1=InStr(sString,".")+1
If InStr(mid(sString,dmarker1),".") Then
dmarker2=InStr(mid(sString,dmarker1),".")

If NOT dmarker2=0 Then
temp=dmarker1+(dmarker2-2)
sString=left(sString,temp)
End If

End If
End If

FormatMoney "$" & FormatNumber(sString,2)
End function


With this function 25.35.45 will return $24.35 and 123.456.789 will return $123.46

Not to mention the usual 12 will return $12.00 and 12.3 will return $12.30
(If this comment was disrespectful, please report it.)

 
10/14/2003 1:14:20 AMScott March

Why not just use the built-in VBScript function FormatCurrency()? Seems you are trying to reinvent the wheel with this one. :-)
(If this comment was disrespectful, please report it.)

 
10/14/2003 10:29:58 AMBrian Reeves

Didn't know about that function at the time of writing it...
(If this comment was disrespectful, please report it.)

 

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.