System.Decimal 구조체

이 문서에서는 이 API에 대한 참조 설명서에 대한 추가 설명서를 제공합니다.

값 형식은 Decimal 양의 79,228,162,514,264,337,593,543,950에 이르는 소수 자릿수를 나타냅니다. 335에서 음수로 79,228,162,514,264,337,593,543,950,335. 기본값은 Decimal 0입니다. 값 형식은 Decimal 많은 수의 정수 및 소수 자릿수가 필요하고 반올림 오류가 없는 재무 계산에 적합합니다. 이 형식은 Decimal 반올림할 필요가 없습니다. 오히려 반올림으로 인한 오류를 최소화합니다. 예를 들어 다음 코드는 1이 아닌 0.9999999999999999999999999999999999999999의 결과를 생성합니다.

decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 0.9999999999999999999999999999 to the console
Console.WriteLine(dividend/divisor * divisor);
let dividend = Decimal.One
let divisor = 3m
// The following displays 0.9999999999999999999999999999 to the console
printfn $"{dividend/divisor * divisor}"
Dim dividend As Decimal = Decimal.One
Dim divisor As Decimal = 3
' The following displays 0.9999999999999999999999999999 to the console
Console.WriteLine(dividend/divisor * divisor)

나누기 및 곱셈의 결과가 메서드에 Round 전달되면 다음 코드와 같이 정밀도가 손실되지 않습니다.

decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 1.00 to the console
Console.WriteLine(Math.Round(dividend/divisor * divisor, 2));
let dividend = Decimal.One
let divisor = 3m
// The following displays 1.00 to the console
printfn $"{Math.Round(dividend/divisor * divisor, 2)}"
Dim dividend As Decimal = Decimal.One
Dim divisor As Decimal = 3
' The following displays 1.00 to the console
Console.WriteLine(Math.Round(dividend/divisor * divisor, 2))

10진수는 부호, 값의 각 숫자가 0에서 9 사이인 숫자 값, 숫자 값의 정수 부분과 소수 부분을 구분하는 부동 소수점 위치를 나타내는 배율 인수로 구성된 부동 소수점 값입니다.

값의 Decimal 이진 표현은 96비트 정수로 구성된 128비트 및 10진수 부분을 지정하는 데 사용되는 부호 및 배율 인수와 같은 항목을 나타내는 32비트 플래그 집합입니다. 따라서 형식(-2 96~296)/10(0~28)) -(2 96-1)이 같MinValue296-1이 같은 MaxValue값의 이진 표현 Decimal 입니다. 값의 Decimal 이진 표현 및 예제에 대한 자세한 내용은 생성자 및 메서드를 GetBits 참조 Decimal(Int32[]) 하세요.

배율 인수는 후행 0도 숫자로 Decimal 유지합니다. 후행 0은 산술 또는 비교 연산의 Decimal 숫자 값에 영향을 미치지 않습니다. 그러나 적절한 형식 문자열이 적용되는 경우 메서드에서 ToString 후행 0을 표시할 수 있습니다.

변환 고려 사항

이 형식은 값을 , , Int32UInt32UInt64Int64UInt16Int16Byte및 값으로 변환 Decimal 하는 SByte메서드를 제공합니다. 이러한 정수 계열 형식 Decimal 에서 확대 변환은 정보를 손실하거나 예외를 throw하지 않는 변환을 확대합니다.

정수 계열 형식으로 Decimal 변환하면 값을 가장 가까운 정수 값으로 반올림 Decimal 하는 축소 변환이 0으로 조정됩니다. C#과 같은 일부 언어는 값 Char 으로의 Decimal 변환도 지원합니다. 이러한 변환의 결과를 대상 형식으로 나타낼 수 없는 경우 예외가 OverflowException throw됩니다.

또한 이 형식은 Decimal 값을 값으로 변환 Decimal 하는 SingleDouble 메서드를 제공합니다. 변환된 값의 DecimalSingleDouble 크기에 대한 정보가 아니라 전체 자릿수를 잃을 수 있는 변환을 축소하거나 축소합니다. 변환은 예외를 throw하지 않습니다.

Single 변환 결과를 로 나타낼 Decimal수 없는 경우 예외를 발생하거나 DoubleDecimal throw OverflowException 하기 위한 변환입니다.

10진수 값에 대한 작업 수행

이 형식은 Decimal 더하기, 빼기, 나누기, 곱하기 및 단항 부정과 같은 표준 수학 연산을 지원합니다. 메서드를 호출하여 값의 이진 표현으로 Decimal 직접 작업할 GetBits 수도 있습니다.

Decimal 값을 비교하려면 표준 숫자 비교 연산자를 사용하거나 또는 Equals 메서드를 CompareTo 호출할 수 있습니다.

클래스의 Math 멤버를 호출하여 숫자의 절대값 가져오기, 두 Decimal 값의 최대값 또는 최소값 결정, 숫자 기호 가져오기, 숫자 반올림 등 다양한 숫자 연산을 수행할 수도 있습니다.

예제

다음 코드 예제에서는 Decimal의 사용법을 보여줍니다.

/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
class PiggyBank {
    protected decimal MyFortune;

    public void AddPenny() {
        MyFortune = Decimal.Add(MyFortune, .01m);
    }

    public decimal Capacity {
        get {
            return Decimal.MaxValue;
        }
    }

    public decimal Dollars {
        get {
            return Decimal.Floor(MyFortune);
        }
    }

    public decimal Cents {
        get {
            return Decimal.Subtract(MyFortune, Decimal.Floor(MyFortune));
        }
    }

    public override string ToString() {
        return MyFortune.ToString("C")+" in piggy bank";
    }
}
/// Keeping my fortune in Decimals to avoid the round-off errors.
type PiggyBank() =
    let mutable myFortune = 0m

    member _.AddPenny() =
        myFortune <- Decimal.Add(myFortune, 0.01m)

    member _.Capacity =
        Decimal.MaxValue

    member _.Dollars =
        Decimal.Floor myFortune

    member _.Cents =
        Decimal.Subtract(myFortune, Decimal.Floor myFortune)

    override _.ToString() =
        $"{myFortune:C} in piggy bank"
' Keeping my fortune in Decimals to avoid the round-off errors.
Class PiggyBank
    Protected MyFortune As Decimal

    Public Sub AddPenny()
        MyFortune = [Decimal].Add(MyFortune, 0.01D)
    End Sub

    Public ReadOnly Property Capacity() As Decimal
        Get
            Return [Decimal].MaxValue
        End Get
    End Property

    Public ReadOnly Property Dollars() As Decimal
        Get
            Return [Decimal].Floor(MyFortune)
        End Get
    End Property

    Public ReadOnly Property Cents() As Decimal
        Get
            Return [Decimal].Subtract(MyFortune, [Decimal].Floor(MyFortune))
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return MyFortune.ToString("C") + " in piggy bank"
    End Function
End Class