FormatPercent [VBA]
Returns a string with a number formatting applied to a numeric expression. A percent sign is appended to the returned string.
This constant, function or object is enabled with the statement Option VBASupport 1 placed before the executable program code in a module.
FormatPercent( expression, [numDigitsAfterDecimal As Integer], [includeLeadingDigit As Integer], _
[useParensForNegativeNumbers As Integer], [groupDigits As Integer] ) As String
String
expression: Required. A numeric expression to be formatted. If expression is a string, then the decimal and thousands separator need to be localized.
numDigitsAfterDecimal: Pilihan. Nilai numerik menentukan jumlah digit yang harus ditampilkan setelah desimal. Jika dihilangkan, baku ke nilai -1, yang berarti bahwa pengaturan baku untuk antarmuka lokasi pengguna harus digunakan.
includeLeadingDigit: Pilihan. A vbTriState enumerasi nilai, menentukan apakah nol depan harus ditampilkan untuk nilai fraksional.
-
vbTrue atau -1: Menampilkan nol di depan.
-
vbFalse or 0: Tidak menampilkan angka nol di depan.
-
vbUseDefault or -2: Use the user interface locale settings. This is the default when omitted.
useParensForNegativeNumbers: Opsional. Sebuah vbTriStatenilai enumerasi menentukan apakah angka negatif harus dimasukkan dalam tanda kurung.
-
vbTrue atau -1: Gunakan tanda kurung untuk angka negatif.
-
vbFalse or 0: Jangan tampilkan tanda kurung.
-
vbUseDefault or -2: Same as vbFalse. This is the default when omitted.
groupDigits: Pilihan. vbTriState nilai pencacahan yang menentukan nomor harus dikelompokkan (menjadi ribuan, dll.), menggunakan pembatas grup yang ditentukan pada pengaturan sistem regional.
-
vbTrue atau -1: Digit grup.
-
vbFalse or 0: Jangan mengelompokkan angka.
-
vbUseDefault or -2: Same as vbFalse. This is the default when omitted.
Sub TestFormatNumber
Const UseComputerRegionalSettings = -1
MsgBox FormatPercent(12.2, NumDigitsAfterDecimal:=2)
MsgBox FormatPercent("-,2", 2, IncludeLeadingDigit:=vbTrue)
MsgBox FormatPercent("-0.2", 2)
MsgBox FormatPercent(-0.2, UseComputerRegionalSettings, UseParensForNegativeNumbers:=vbTrue)
MsgBox FormatPercent("-0,2", UseComputerRegionalSettings, vbUseDefault, vbTrue)
MsgBox FormatPercent("-12345678", -1, vbUseDefault, vbUseDefault, GroupDigits:=vbTrue)
End Sub