본문 바로가기
VBA 라이브러리

FormatCurrency 함수

2023. 8. 22.

FormatCurrency 함수

시스템 제어판에 정의된 통화 기호를 사용하여 통화 값으로 서식이 지정된 식을 반환합니다.

Syntax
FormatCurrency(Expression, [ NumDigitsAfterDecimal, [ IncludeLeadingDigit, [ UseParensForNegativeNumbers, [ GroupDigits ]]]])

 

FormatCurrency 함수 구문은 다음과 같은 구성 요소로 되어 있습니다.

 

Expression : 필수입니다. 서식을 지정할 표현식입니다.
NumdigitsAfterDecimal : 선택 사항입니다. 소수점 오른쪽 몇 자리까지 표시할지를 나타내는 숫자 값입니다. 기본값은 -1이며, 이는 컴퓨터의 지역 설정이 사용됨을 나타냅니다.

IncludeLeadingDigit : 선택 사항입니다. 분수 값에 선행 0을 표시할지 여부를 나타내는 삼진법 상수입니다. 값은 설정 섹션을 참조하십시오.

UseParensForNegativeNumbers : 선택 사항입니다. 괄호 안에 음수 값을 넣을지 여부를 나타내는 Tristate 상수입니다. 값은 설정 섹션을 참조하십시오.

GroupDigits : 선택 사항입니다. 컴퓨터의 지역 설정에 지정된 그룹 구분 기호를 사용하여 숫자를 그룹화할지 여부를 나타내는 삼진법 상수입니다. 값은 설정 섹션을 참조하십시오.

 

설정

IncludeLeadingDigit, UseParensForNegativeNumbers 및 GroupDigits 인수의 설정은 다음과 같습니다

 

참고
하나 이상의 선택적 인수가 생략된 경우 생략된 인수의 값은 컴퓨터의 지역 설정에 따라 제공됩니다. 통화 값에 대한 통화 기호의 위치는 시스템의 국가별 설정에 따라 결정됩니다.

메모
숫자 탭에서 제공되는 선행 0을 제외한 모든 설정 정보는 지역별 설정 통화 탭에서 제공됩니다.

 

예제 코드 - 1

Function FormatCurrencyWithLabel(ByVal value As Double, ByVal label As String, Optional ByVal numDigits As Integer = 2) As String
    Dim formattedCurrency As String
    formattedCurrency = FormatCurrency(value, NumDigitsAfterDecimal:=numDigits)
    FormatCurrencyWithLabel = label & ": " & formattedCurrency
End Function

Sub TestFormatCurrency()
    Dim currencyValue As Double
    currencyValue = 12345.6789

    Debug.Print FormatCurrencyWithLabel(currencyValue, "기본 통화 형식")
    Debug.Print FormatCurrencyWithLabel(currencyValue, "소수점 1자리 통화 형식", 1)
    Debug.Print FormatCurrencyWithLabel(currencyValue, "소수점 3자리 통화 형식", 3)
End Sub

' Output
' 기본 통화 형식: $12,345.68
' 소수점 1자리 통화 형식: $12,345.7
' 소수점 3자리 통화 형식: $12,345.679

예제 코드 - 2

Function FormatCurrencyCustom(ByVal value As Double, ByVal label As String, Optional ByVal numDigits As Integer = 2, Optional ByVal includeLeadingDigit As Boolean = True) As String
    Dim formattedCurrency As String
    formattedCurrency = FormatCurrency(value, NumDigitsAfterDecimal:=numDigits, includeLeadingDigit:=includeLeadingDigit)
    FormatCurrencyCustom = label & ": " & formattedCurrency
End Function

Sub TestFormatCurrencyIncludeLeadingDigit()
    Dim currencyValue As Double
    currencyValue = 0.9471

    Debug.Print FormatCurrencyCustom(currencyValue, "선행 0 미포함 통화 형식", includeLeadingDigit:=False)
    Debug.Print FormatCurrencyCustom(currencyValue, "선행 0 포함 통화 형식", includeLeadingDigit:=True)

    Dim numDigits As Integer
    numDigits = 3
    Debug.Print FormatCurrencyCustom(currencyValue, "선행 0 없음", numDigits, False)
    Debug.Print FormatCurrencyCustom(currencyValue, "선행 0 포함", numDigits, True)
End Sub

' Output
' 선행 0 미포함 통화 형식: $.95
' 선행 0 포함 통화 형식: $0.95
' 선행 0 없음: $.947
' 선행 0 포함: $0.947

예제 코드 - 3

Public Function FormatCurrencyWithCustomOptions(ByVal currencyValue As Double, Optional numDigits As Integer = 2, Optional includeLeadingDigit As Boolean = True, Optional useParensForNegativeNumbers As Boolean = False) As String
    FormatCurrencyWithCustomOptions = FormatCurrency(currencyValue, numDigitsAfterDecimal:=numDigits, includeLeadingDigit:=includeLeadingDigit, useParensForNegativeNumbers:=useParensForNegativeNumbers)
End Function
Sub Test_FormatCurrency_IncludeLeadingDigit_Options()
    Dim currencyValue As Double
    currencyValue = -0.5937

    Dim formattedCurrency As String
    
    Debug.Print "기본 옵션: " & FormatCurrencyWithCustomOptions(currencyValue)
    Debug.Print "선행 0 미포함: " & FormatCurrencyWithCustomOptions(currencyValue, includeLeadingDigit:=False)
    Debug.Print "소수점 아래 3자리, 선행 0 포함: " & FormatCurrencyWithCustomOptions(currencyValue, 3)
    Debug.Print "소수점 아래 3자리, 선행 0 미포함: " & FormatCurrencyWithCustomOptions(currencyValue, 3, False)
    Debug.Print "음수 (), 선행 0 없음: " & FormatCurrencyWithCustomOptions(currencyValue, 3, False, True)
    Debug.Print "음수 (-), 선행 0 없음: " & FormatCurrencyWithCustomOptions(currencyValue, 3, False, False)
    Debug.Print "음수 (), 선행 0 포함: " & FormatCurrencyWithCustomOptions(currencyValue, 3, True, True)
    Debug.Print "음수 (-), 선행 0 포함: " & FormatCurrencyWithCustomOptions(currencyValue, 3, True, False)
End Sub

' Output
' 기본 옵션: -$0.59
' 선행 0 미포함: -$.59
' 소수점 아래 3자리, 선행 0 포함: -$0.594
' 소수점 아래 3자리, 선행 0 미포함: -$.594
' 음수 (), 선행 0 없음: ($.594)
' 음수 (-), 선행 0 없음: -$.594
' 음수 (), 선행 0 포함: ($0.594)
' 음수 (-), 선행 0 포함: -$0.594

예제 코드 - 4

Public Function FormatCurrencyWithCustomOptions(ByVal currencyValue As Double, _
                                                Optional numDigits As Integer = 2, _
                                                Optional includeLeadingDigit As Boolean = True, _
                                                Optional useParensForNegativeNumbers As Boolean = False, _
                                                Optional groupDigits As Boolean = True) As String
    FormatCurrencyWithCustomOptions = FormatCurrency(currencyValue, _
                                                      numDigitsAfterDecimal:=numDigits, _
                                                      includeLeadingDigit:=includeLeadingDigit, _
                                                      useParensForNegativeNumbers:=useParensForNegativeNumbers, _
                                                      groupDigits:=groupDigits)
End Function


Sub TestFormatCurrency()
    Dim currencyValue As Double
    currencyValue = -3978.83687397755
    Dim numDigits As Integer
    numDigits = 4

    Debug.Print "기본 통화 형식: " & FormatCurrencyWithCustomOptions(currencyValue)
    Debug.Print "소수점 " & numDigits & "자리 통화 형식: " & FormatCurrencyWithCustomOptions(currencyValue, numDigits:=numDigits)
    Debug.Print "괄호로 음수 표시: " & FormatCurrencyWithCustomOptions(currencyValue, numDigits:=numDigits, useParensForNegativeNumbers:=True)
    Debug.Print "괄호 없이 음수 표시: " & FormatCurrencyWithCustomOptions(currencyValue, numDigits:=numDigits, useParensForNegativeNumbers:=False)
    Debug.Print "천단위 구분기호 미사용: " & FormatCurrencyWithCustomOptions(currencyValue, numDigits:=numDigits, useParensForNegativeNumbers:=True, groupDigits:=False)
    Debug.Print "천단위 구분기호 사용: " & FormatCurrencyWithCustomOptions(currencyValue, numDigits:=numDigits, useParensForNegativeNumbers:=True, groupDigits:=True)
End Sub

' Output
' 기본 통화 형식: -$3,978.84
' 소수점 4자리 통화 형식: -$3,978.8369
' 괄호로 음수 표시: ($3,978.8369)
' 괄호 없이 음수 표시: -$3,978.8369
' 천단위 구분기호 미사용: ($3978.8369)
' 천단위 구분기호 사용: ($3,978.8369)

도움말 출처

 

 

FormatCurrency function (Visual Basic for Applications)

Office VBA reference topic

learn.microsoft.com

 

'VBA 라이브러리' 카테고리의 다른 글

FormatNumber 함수  (0) 2023.08.23
FormatDateTime 함수  (0) 2023.08.23
Format 함수  (0) 2023.08.22
GetObject 함수  (0) 2023.08.06
CreateObject 함수  (0) 2023.08.06