Formatting numbers with Classic ASP

Format Numbers – Reference

Frequently you want a number to appear in a certain format. The most commands requests are for a set total number of digits and a set number of digits to the right of the decimal place. Less frequently there is a call for negative amounts displayed in parenthesis or that there should be leading zeros.

The FormatNumber function takes the contents of a number type variable and returns the contents in the specified format.

Syntax: FormatNumber(expression, iDigits, bleadingDigit, bParen, bGroupDigits)

argument meaning
expression the variable holding the raw number
iDigits number of digits to right of decimal point
bleadingDigit 1 for leading zeros
0 for no leading zeros
bParen 1 for parenthesis around negative numbers
0 for no parenthesis around negative numbers
bGroupDigits 1 to display numbers as per regional settings in the Control Panel
0 to over-ride settings in the Control Panel

<html><head>
<TITLE>formatnumbers2.asp</TITLE>
</head><body bgcolor=”#FFFFFF”>
<%
 My ASP formatting number sample
mynumber=123.4567
response.write “<hr>” & mynumber & “<br>”
response.write “formatnumber(mynumber,0)” & “<br>”
response.write formatnumber(mynumber,0) & “<hr>”
response.write “formatnumber(mynumber,2)” & “<br>”
response.write formatnumber(mynumber,2) & “<hr>”
response.write “formatnumber(mynumber,6)” & “<br>”
response.write formatnumber(mynumber,6) & “<hr>”

mynumber=.4567
response.write mynumber & “<br>”
0 means means no leading zeroes
response.write “formatnumber(mynumber,2,0)” & “<br>”
response.write formatnumber(mynumber,2,0) & “<hr>”
1 means means pad with leading zeroes
response.write “formatnumber(mynumber,2,1)” & “<br>”
response.write formatnumber(mynumber,2,1) & “<hr>”

mynumber=-123.4567
response.write mynumber & “<br>”
0 means means no parentheses for negative numbers
response.write “formatnumber(mynumber,2,0,0)” & “<br>”
response.write formatnumber(mynumber,2,0,0) & “<hr>”
1 means means yes parentheses for negative numbers
response.write “formatnumber(mynumber,2,0,1)” & “<br>”
response.write formatnumber(mynumber,2,0,1) & “<hr>”
%>
</body></html>

Advertisements