Format函数汇总

Format: Returns a string formatted according to instructions contained in a format String expression.

Parameters
Expression
Required. Any valid expression.
Style
Optional. A valid named or user-defined format String expression.
Settings

For information on how to create the Style argument, see the appropriate topic listed below:

To formatDo this
NumbersUse predefined numeric formats or create user-defined numeric formats.
Dates and timesUse predefined date/time formats or create user-defined date/time formats.
Date and time serial numbersUse date and time formats or numeric formats.
The following table identifies the predefined numeric format names. These may be used by name as the style argument for the Format function:

Format nameDescription
General Number, G, or g Displays number with no thousand separator.
Currency, C, or c Displays number with thousand separator, if appropriate; display two digits to the right of the decimal separator. Output is based on system locale settings.
Fixed, F, or f Displays at least one digit to the left and two digits to the right of the decimal separator.
Standard, N, or n Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.
Percent, P, or p Displays number multiplied by 100 with a percent sign (%) appended to the right; always display two digits to the right of the decimal separator.
Scientific, E, or e Uses standard scientific notation.
D, or d Displays number as a string that contains the value of the number in Decimal (base 10) format. This option is supported for integral types (Byte, Short, Integer, Long) only.
X, or x Displays number as a string that contains the value of the number in Hexadecimal (base 16) format. This option is supported for integral types (Byte, Short, Integer, Long) only.
Yes/NoDisplays No if number is 0; otherwise, displays Yes.
True/FalseDisplays False if number is 0; otherwise, displays True.
On/OffDisplays Off if number is 0; otherwise, displays On.

If you try to format a number without specifying Style, the Format function provides functionality similar to the Str function, although it is internationally aware. However, positive numbers formatted as strings using the Format function don't include a leading space reserved for the sign of the value; those converted using the Str function retain the leading space.

Remarks

If you are formatting a nonlocalized numeric string, you should use a user-defined numeric format to ensure that you get the look you want.

The String.Format method also provides similar functionality.

Example

This example shows various uses of the Format function to format values using both String formats and user-defined formats. For the date separator (/), time separator (:), and the AM/PM indicators (t and tt), the actual formatted output displayed by your system depends on the locale settings the code is using. When times and dates are displayed in the development environment, the short time format and short date format of the code locale are used.

Dim MyDateTime As Date = #1/27/2001 5:04:23 PM#
Dim MyStr As String
' Returns current system time in the system-defined long time format.
MyStr = Format(Now(), "Long Time")
' Returns current system date in the system-defined long date format.
MyStr = Format(Now(), "Long Date")
' Also returns current system date in the system-defined long date 
' format, using the single letter code for the format.
MyStr = Format(Now(), "D")
' Returns the value of MyDateTime in user-defined date/time formats.
MyStr = Format(MyDateTime, "h:m:s")   ' Returns "5:4:23".
MyStr = Format(MyDateTime, "hh:mm:ss tt")   ' Returns "05:04:23 PM".
MyStr = Format(MyDateTime, "dddd, MMM d yyyy")   ' Returns "Saturday,
   ' Jan 27 2001".
MyStr = Format(MyDateTime, "HH:mm:ss")   ' Returns "17:04:23"

MyStr = Format(23)   ' Returns "23".
' User-defined numeric formats.
MyStr = Format(5459.4, "##,##0.00")   ' Returns "5,459.40".
MyStr = Format(334.9, "###0.00")   ' Returns "334.90".
MyStr = Format(5, "0.00%")   ' Returns "500.00%".

MyStr = Format(23.99, "$##,##0.00")   ' Returns "$23.00".

Format$: Provides both predefined numeric formats and user-defined formats.

Examples:

Format$(12345.6789, "Currency")
$12345.67
Format$(0.6789, "Fixed")

0.67

(At least one digit to the left and two digits to the right of the period.)

Format$(0.1234, "Percent")
12.34%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值