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

FormatNumber 함수

2023. 8. 23.

FormatNumber 함수

 

숫자로 형식이 지정된 표현식을 반환합니다.

 

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

 

* FormatCurrency 함수와 똑같은 매개변수를 가지고 있습니다.

 

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

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

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

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

 

설정
IncludeLeadingDigit, UseParensForNegativeNumbers, GroupDigits의 인수 설정은 아래와 같습니다.

 

참고
하나 이상의 선택적 인수가 생략된 경우 생략된 인수의 값은 컴퓨터의 국가별 설정에 의해 제공됩니다.

메모
모든 설정 정보는 국가별 설정 번호 탭에서 제공됩니다.

 

예제 코드 - 1

Sub ExampleFormatNumberCurrency()
    Dim numberValue As Double
    numberValue = 1234.56

    Dim formattedNumber As String
    Dim formattedCurrency As String

    ' FormatNumber 함수를 사용하여 소수점 1자리로 서식 적용 (소수점 1자리로 반올림)
    formattedNumber = FormatNumber(numberValue, 1)

    ' FormatCurrency 함수를 사용하여 소수점 1자리와 통화 기호로 서식 적용 (소수점 1자리로 반올림)
    formattedCurrency = FormatCurrency(numberValue, 1)

    Debug.Print "FormatNumber 결과: " & formattedNumber ' 결과값: 1,234.6
    Debug.Print "FormatCurrency 결과: " & formattedCurrency ' 결과값: $1,234.6 (시스템 설정에 따라 통화 기호가 다르게 표시될 수 있습니다)
    
    numberValue = 1234.54
    
    ' FormatNumber 함수를 사용하여 소수점 1자리로 서식 적용 - (소수점 1자리 이하 버림)
    formattedNumber = FormatNumber(numberValue, 1)

    ' FormatCurrency 함수를 사용하여 소수점 1자리와 통화 기호로 서식 적용 (소수점 1자리 이하 버림)
    formattedCurrency = FormatCurrency(numberValue, 1)

    Debug.Print "FormatNumber 결과: " & formattedNumber ' 결과값: 1,234.5
    Debug.Print "FormatCurrency 결과: " & formattedCurrency ' 결과값: $1,234.5 (시스템 설정에 따라 통화 기호가 다르게 표시될 수 있습니다)
End Sub

' Output
' FormatNumber 결과: 1,234.6
' FormatCurrency 결과: $1,234.6
' FormatNumber 결과: 1,234.5
' FormatCurrency 결과: $1,234.5

예제 코드 - 2

Sub ExampleFormatNumberWithParensForNegative()
    Dim numberValue As Double
    numberValue = -1234.56

    Dim formattedNumber As String
    Dim formattedCurrency As String
    
    ' FormatNumber 함수를 사용하여 소수점 1자리로 서식 적용 및 음수를 괄호로 표시
    formattedNumber = FormatNumber(numberValue, 1, , vbTrue)
    formattedCurrency = FormatCurrency(numberValue, 1, , vbTrue)
    
    Debug.Print "FormatNumber 결과: " & formattedNumber    ' 결과값: (1,234.6)
    Debug.Print "FormatCurrency 결과: " & formattedCurrency    ' 결과값: ($1,234.6)
    
    ' FormatNumber 함수를 사용하여 소수점 1자리로 서식 적용 및 음수를 괄호로 표시 안함
    formattedNumber = FormatNumber(numberValue, 1, , vbFalse)
    formattedCurrency = FormatCurrency(numberValue, 1, , vbFalse)
    
    Debug.Print "FormatNumber 결과: " & formattedNumber    ' 결과값: -1,234.6
    Debug.Print "FormatCurrency 결과: " & formattedCurrency    ' 결과값: -$1,234.6
End Sub

' Output
' FormatNumber 결과: (1,234.6)
' FormatCurrency 결과: ($1,234.6)
' FormatNumber 결과: -1,234.6
' FormatCurrency 결과: -$1,234.6

 

 

 

 

* FormatNumber 함수와 FormatCurrency 함수는 서식을 제외하면 같은 값을 반환합니다.때에 따라 적절한 함수를 선택하여 사용하면 됩니다.

 

도움말 출처

 

 

FormatNumber function (Visual Basic for Applications)

Office VBA reference topic

learn.microsoft.com

 

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

InStr 함수  (0) 2023.08.23
FormatPercent 함수  (0) 2023.08.23
FormatDateTime 함수  (0) 2023.08.23
FormatCurrency 함수  (0) 2023.08.22
Format 함수  (0) 2023.08.22