Formatnumber function of asp
BY-huizaizai
FormatNumber Function
See
FormatCurrency function | FormatDateTime function | FormatPercent Function
Requirements
Version 2
Return expression, which has been formatted as a value.
FormatNumber (
Expression [, NumDigitsAfterDecimal [, IncludeLeadingDigit [, UseParensForNegativeNumbers [, GroupDigits]
)
Arguments
Expression
Required. The expression to be formatted.
NumDigitsAfterDecimal
Optional. The number of digits to the right of the decimal point. The default value is-1, indicating that the computer's regional settings are used.
IncludeLeadingDigit
Optional. A three-state constant that indicates whether to display zero before the decimal point of a small number. For values, see the "Settings" section.
UseParensForNegativeNumbers
Optional. A three-state constant that indicates whether to place a negative value in parentheses. For values, see the "Settings" section.
GroupDigits
Optional. A three-state constant that indicates whether to use the numeric grouping symbol specified in the computer region settings to group numbers. For values, see the "Settings" section.
Set
The deleadingdigit, UseParensForNegativeNumbers, and GroupDigits parameters can have the following values:
Constant Value description
TristateTrue-1 True
TristateFalse 0 False
TristateUseDefault-2 uses the settings in computer region settings.
Description
When one or more optional parameters are omitted, the value of the omitted parameter is provided by the computer region settings.
Note that all settings are taken from the numbers tab of the region settings.
The following example uses the FormatNumber function to format a value into a number with four decimal places:
Function FormatNumberDemo
Dim MyAngle, MySecant, MyNumber
MyAngle = 1.3 'defines an Angle Using radians.
MySecant = 1/Cos (MyAngle) 'calculates the cut value.
FormatNumberDemo = FormatNumber (MySecant, 4) 'format MySecant to the number of four decimal places.
VBScript has two negative numbers used to format numbers. Using the Formatnumber () function, you can specify the number of digits displayed after the decimal point, whether to display leading zero, whether to use spaces when displaying negative numbers, and whether to use separators. The following are examples and returned values:
<% = Formatnumber (6665.8999) %>
6,665.90
<% = Formatnumber ("6665.8999") %>
6,665.90
<% = Formatnumber (6665.8999, 3) %>
6,665.900
<% = Formatnumber (-6665.8999, 3,-1,-) %>
(6665.900)
Note that the second example uses a string, and the second is not a number. When a string can be interpreted as a number, you can use this string as a parameter of the Formatnumber () function. If the string cannot be interpreted as a number, an error is returned.
In the last example, all parameters of the Formatnumber () function are used:
The first parameter (-6665.8999) specifies the number to be formatted.
The second parameter (3) specifies the number of digits displayed after the decimal point.
The third parameter (-1) Specifies whether to display the leading zero.
The fourth parameter (-1) Specifies whether to use parentheses for negative numbers.
The last parameter (0) Specifies whether to display the delimiter.
Note:
Many VBScript functions use three-state constants. The third, fourth, and fifth parameters of the Formatnumber () function can accept three-state constants or their values. Three-state constants are listed below:
TristateTrue-1 True
TristateFalse 0 False
TristateUseDefault-2 Use region settings
You can also specify that a specific attribute of these formats depends on the region settings in the Windows Control Panel.
If you need to format the percentage, use the Formatpercent () function ():
<% = Formatpercent (12) %>
1,200.00%
<% = Formatpercent ("12") %>
1,200.00%
This function times its first parameter by 100 and adds a percent sign to the result. The Formatpercent () function can also accept many additional parameters to change the output format.
FormatPercent Function
See
FormatCurrency function | FormatDateTime function | FormatNumber Function
Requirements
Version 2
Return expression, which has been formatted as the percentage of % characters trailing (multiplied by 100 ).
FormatPercent (expression [, NumDigitsAfterDecimal [, IncludeLeadingDigit [, UseParensForNegativeNumbers [, GroupDigits])
The syntax of the FormatPercent function has the following parameters:
Parameters
Expression
Required. The expression to be formatted.
NumDigitsAfterDecimal
Optional. The number of digits to the right of the decimal point. The default value is-1, indicating that the computer's regional settings are used.
IncludeLeadingDigit
Optional. A three-state constant that indicates whether to display zero before the decimal point of a small number. For values, see the "Settings" section.
UseParensForNegativeNumbers
Optional. A three-state constant that indicates whether to place a negative value in parentheses. For values, see the "Settings" section.
GroupDigits
Optional. A three-state constant that indicates whether to use the numeric grouping symbol specified in the computer region settings to group numbers. For values, see the "Settings" section.
Set
The deleadingdigit, UseParensForNegativeNumbers, and GroupDigits parameters can have the following values:
Constant Value description
TristateTrue-1 True
TristateFalse 0 False
TristateUseDefault-2 uses the settings in computer region settings.
Description
When one or more optional parameters are omitted, the value of the omitted parameter is provided by the computer region settings.
Note that all settings are taken from the numbers tab of the region settings.
The following example uses the FormatPercent function to format the expression as a percentage:
Dim MyPercent
MyPercent = FormatPercent (2/32) 'MyPercent contains 6.25%.
//////////////////////////////////
Function ******************************
FormatDateTime (Date [, NamedFormat])
//////////////////////////////////
Value of NamedFormat :******************************
Constant Value description
---------------------------------------------------------------------------------
VbGeneralDate 0 displays the date and/or time. If there is a date part, it is displayed as the short date format. If there is a time part, it is displayed as a long time format. If all exist, all parts are displayed.
VbLongDate 1 displays the date in the long date format specified in the computer region settings.
Vb1_date 2 displays the date in the short date format specified in the computer region settings.
VbLongTime 3 displays the time in the specified time format in the computer region settings.
Vb1_time 4 uses the 24-hour format (hh: mm) to display the time.
Formatdate functions of ASP
BY-huizaizai
FormatDateTime Function
See
FormatCurrency function | FormatNumber function | FormatPercent Function
Requirements
Version 2
Return expression, which has been formatted as a date or time.
FormatDateTime (Date [, NamedFormat])
Parameters
Date
Required. The date expression to be formatted.
NamedFormat
Optional. Indicates the value of the date/time format used. If it is omitted, vbGeneralDate is used.
Set
The NamedFormat parameter can have the following values:
Constant Value description
VbGeneralDate 0 displays the date and/or time. If there is a date part, it is displayed as the short date format. If there is a time part, it is displayed as a long time format. If all exist, all parts are displayed.
VbLongDate 1 displays the date in the long date format specified in the computer region settings.
Vb1_date 2 displays the date in the short date format specified in the computer region settings.
VbLongTime 3 displays the time in the specified time format in the computer region settings.
Vb1_time 4 uses the 24-hour format (hh: mm) to display the time.
Description
The following example uses the FormatDateTime function to format the expression as a long date type and assign it to MyDateTime:
Function GetCurrentDate
'Formatdatetime format the date type to the long date type.
GetCurrentDate = FormatDateTime (Date, 1)
End Function
If there is a number 498.8573945, how can we format it into two small pieces of data? All ASP users know that in VbScript, we can call FormatNumber, that is, using FormatNumber (498.8573945, 2) to output: 498.86.
Since JavaScript does not have this function, how can we implement it? The following figure shows the function: <script language = javascript SRC = "FormatNumber. javaScript "> </SCRIPT> can be added to your HTML page.
<! -- # Include file = "FormatNumber. js" --> Add your ASP page; or use
<? Include ("FormatNumber. js")?> Add to the PHP page; or use
<% @ # Include file = "FormatNumber. js" %> Add to the JSP page.
This FormatNumber sub-function is as follows:
<Script language = javascript>
<! --
Function FormatNumber (srcStr, nAfterDot ){
Var srcStr, nAfterDot;
Var resultStr, nTen;
SrcStr = "" + srcStr + "";
StrLen = srcStr. length;
DotPos = srcStr. indexOf (".", 0 );
If (dotPos =-1 ){
ResultStr = srcStr + ".";
For (I = 0; I <nAfterDot; I ++ ){
ResultStr = resultStr + "0 ";
}
Return resultStr;
}
Else {
If (strLen-dotPos-1)> = nAfterDot ){
NAfter = dotPos + nAfterDot + 1;
NTen = 1;
For (j = 0; j <nAfterDot; j ++ ){
NTen = nTen * 10;
}
ResultStr = Math. round (parseFloat (srcStr) * nTen)/nTen;
Return resultStr;
}
Else {
ResultStr = srcStr;
For (I = 0; I <(nAfterDot-strLen + dotPos + 1); I ++ ){
ResultStr = resultStr + "0 ";
}
Return resultStr;
}
}
}