Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

struct System.​Decimal

Assembly: System.Runtime

Implemented Interfaces

Represents a decimal floating-point number.

Fields and Constants

public static readonly decimal
MaxValue
Represents the largest possible value of <see cref="T:System.Decimal" /> . This field is constant and read-only.
public static readonly decimal
MinusOne
Represents the number negative one (-1).
public static readonly decimal
MinValue
Represents the smallest possible value of <see cref="T:System.Decimal" /> . This field is constant and read-only.
public static readonly decimal
One
Represents the number one (1).
public static readonly decimal
Zero
Represents the number zero (0).

Properties

public byte
Scale
Gets the scaling factor of the decimal, which is a number from 0 to 28 that represents the number of decimal digits.

Methods

public static decimal
Abs​(decimal value)
Computes the absolute of a value.
Returns The absolute of <paramref name="value" /> .
value The value for which to get its absolute.
public static decimal
Add​(decimal d1, decimal d2)
Adds two specified <see cref="T:System.Decimal" /> values.
Returns The sum of <paramref name="d1" /> and <paramref name="d2" /> .
d1 The first value to add.
d2 The second value to add.
public static decimal
Ceiling​(decimal d)
Returns the smallest integral value that is greater than or equal to the specified decimal number.
Returns The smallest integral value that is greater than or equal to the <paramref name="d" /> parameter. Note that this method returns a <see cref="T:System.Decimal" /> instead of an integral type.
d A decimal number.
public static decimal
Clamp​(decimal value, decimal min, decimal max)
Clamps a value to an inclusive minimum and maximum value.
Returns The result of clamping <paramref name="value" /> to the inclusive range of <paramref name="min" /> and <paramref name="max" /> .
value The value to clamp.
min The inclusive minimum to which <paramref name="value" /> should clamp.
max The inclusive maximum to which <paramref name="value" /> should clamp.
public static int
Compare​(decimal d1, decimal d2)
Compares two specified <see cref="T:System.Decimal" /> values.
Returns A signed number indicating the relative values of <paramref name="d1" /> and <paramref name="d2" /> . <list type="table"> <listheader> <term> Return value</term> <description> Meaning</description> </listheader> <item> <term> Less than zero</term> <description> <paramref name="d1" /> is less than <paramref name="d2" />.</description> </item> <item> <term> Zero</term> <description> <paramref name="d1" /> and <paramref name="d2" /> are equal.</description> </item> <item> <term> Greater than zero</term> <description> <paramref name="d1" /> is greater than <paramref name="d2" />.</description> </item> </list>
d1 The first value to compare.
d2 The second value to compare.
public int
CompareTo​(decimal value)
Compares this instance to a specified <see cref="T:System.Decimal" /> object and returns a comparison of their relative values.
Returns A signed number indicating the relative values of this instance and <paramref name="value" /> . <list type="table"> <listheader> <term> Return value</term> <description> Meaning</description> </listheader> <item> <term> Less than zero</term> <description> This instance is less than <paramref name="value" />.</description> </item> <item> <term> Zero</term> <description> This instance is equal to <paramref name="value" />.</description> </item> <item> <term> Greater than zero</term> <description> This instance is greater than <paramref name="value" />.</description> </item> </list>
value The object to compare with this instance.
public int
CompareTo​(object value)
Compares this instance to a specified object and returns a comparison of their relative values.
Returns A signed number indicating the relative values of this instance and <paramref name="value" /> . <list type="table"> <listheader> <term> Return value</term> <description> Meaning</description> </listheader> <item> <term> Less than zero</term> <description> This instance is less than <paramref name="value" />.</description> </item> <item> <term> Zero</term> <description> This instance is equal to <paramref name="value" />.</description> </item> <item> <term> Greater than zero</term> <description> This instance is greater than <paramref name="value" />, or <paramref name="value" /> is <see langword="null" />.</description> </item> </list>
value The object to compare with this instance, or <see langword="null" /> .
public static TInteger
ConvertToInteger​(decimal value)
Converts a value to a specified integer type using saturation on overflow
Returns An instance of <code data-dev-comment-type="typeparamref">TInteger</code> created from <code data-dev-comment-type="paramref">value</code> .
value The value to be converted.
public static TInteger
ConvertToIntegerNative​(decimal value)
Converts a value to a specified integer type using platform specific behavior on overflow.
Returns An instance of <code data-dev-comment-type="typeparamref">TInteger</code> created from <code data-dev-comment-type="paramref">value</code> .
value The value to be converted.
public static decimal
CopySign​(decimal value, decimal sign)
Copies the sign of a value to the sign of another value.
Returns A value with the magnitude of <paramref name="value" /> and the sign of <paramref name="sign" /> .
value The value whose magnitude is used in the result.
sign The value whose sign is used in the result.
public static decimal
CreateChecked​(TOther value)
Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.
Returns An instance of <typeparamref name="TSelf" /> created from <paramref name="value" /> .
value The value that's used to create the instance of <typeparamref name="TSelf" /> .
public static decimal
CreateSaturating​(TOther value)
Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.
Returns An instance of <typeparamref name="TSelf" /> created from <paramref name="value" /> , saturating if <paramref name="value" /> falls outside the representable range of <typeparamref name="TSelf" /> .
value The value that's used to create the instance of <typeparamref name="TSelf" /> .
public static decimal
CreateTruncating​(TOther value)
Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.
Returns An instance of <typeparamref name="TSelf" /> created from <paramref name="value" /> , truncating if <paramref name="value" /> falls outside the representable range of <typeparamref name="TSelf" /> .
value The value that's used to create the instance of <typeparamref name="TSelf" /> .
public static decimal
Divide​(decimal d1, decimal d2)
Divides two specified <see cref="T:System.Decimal" /> values.
Returns The result of dividing <paramref name="d1" /> by <paramref name="d2" /> .
d1 The dividend.
d2 The divisor.
public bool
Equals​(decimal value)
Returns a value indicating whether this instance and a specified <see cref="T:System.Decimal" /> object represent the same value.
Returns <see langword="true" /> if <paramref name="value" /> is equal to this instance; otherwise, <see langword="false" /> .
value An object to compare to this instance.
public static bool
Equals​(decimal d1, decimal d2)
Returns a value indicating whether two specified instances of <see cref="T:System.Decimal" /> represent the same value.
Returns <see langword="true" /> if <paramref name="d1" /> and <paramref name="d2" /> are equal; otherwise, <see langword="false" /> .
d1 The first value to compare.
d2 The second value to compare.
public bool
Equals​(object value)
Returns a value indicating whether this instance and a specified <see cref="T:System.Object" /> represent the same type and value.
Returns <see langword="true" /> if <paramref name="value" /> is a <see cref="T:System.Decimal" /> and equal to this instance; otherwise, <see langword="false" /> .
value The object to compare with this instance.
public static decimal
Floor​(decimal d)
Rounds a specified <see cref="T:System.Decimal" /> number to the closest integer toward negative infinity.
Returns If <paramref name="d" /> has a fractional part, the next whole <see cref="T:System.Decimal" /> number toward negative infinity that is less than <paramref name="d" /> . -or- If <paramref name="d" /> doesn't have a fractional part, <paramref name="d" /> is returned unchanged. Note that the method returns an integral value of type <see cref="T:System.Decimal" /> .
d The value to round.
public static decimal
FromOACurrency​(long cy)
Converts the specified 64-bit signed integer, which contains an OLE Automation Currency value, to the equivalent <see cref="T:System.Decimal" /> value.
Returns A <see cref="T:System.Decimal" /> that contains the equivalent of <paramref name="cy" /> .
cy An OLE Automation Currency value.
public static int[]
GetBits​(decimal d)
Converts the value of a specified instance of <see cref="T:System.Decimal" /> to its equivalent binary representation.
Returns A 32-bit signed integer array with four elements that contain the binary representation of <paramref name="d" /> .
d The value to convert.
public static int
GetBits​(decimal d, Span<​int> destination)
Converts the value of a specified instance of <see cref="T:System.Decimal" /> to its equivalent binary representation.
Returns <see langword="4" /> , which is the number of integers in the binary representation.
d The value to convert.
destination The span into which to store the four-integer binary representation.
public int
GetHashCode​()
Returns the hash code for this instance.
Returns A 32-bit signed integer hash code.
public TypeCode
GetTypeCode​()
Returns the <see cref="T:System.TypeCode" /> for value type <see cref="T:System.Decimal" /> .
Returns The enumerated constant <see cref="F:System.TypeCode.Decimal" /> .
public static bool
IsCanonical​(decimal value)
Determines if a value is in its canonical representation.
Returns <see langword="true" /> if <paramref name="value" /> is in its canonical representation; otherwise, <see langword="false" /> .
value The value to be checked.
public static bool
IsEvenInteger​(decimal value)
Determines if a value represents an even integral number.
Returns <see langword="true" /> if <paramref name="value" /> is an even integer; otherwise, <see langword="false" /> .
value The value to be checked.
public static bool
IsInteger​(decimal value)
Determines if a value represents an integral number.
Returns <see langword="true" /> if <paramref name="value" /> is an integer; otherwise, <see langword="false" /> .
value The value to be checked.
public static bool
IsNegative​(decimal value)
Determines if a value is negative.
Returns <see langword="true" /> if <paramref name="value" /> is negative; otherwise, <see langword="false" /> .
value The value to be checked.
public static bool
IsOddInteger​(decimal value)
Determines if a value represents an odd integral number.
Returns <see langword="true" /> if <paramref name="value" /> is an odd integer; otherwise, <see langword="false" /> .
value The value to be checked.
public static bool
IsPositive​(decimal value)
Determines if a value is positive.
Returns <see langword="true" /> if <paramref name="value" /> is positive; otherwise, <see langword="false" /> .
value The value to be checked.
public static decimal
Max​(decimal x, decimal y)
Compares two values to compute which is greater.
Returns <paramref name="x" /> if it is greater than <paramref name="y" /> ; otherwise, <paramref name="y" /> .
x The value to compare with <paramref name="y" /> .
y The value to compare with <paramref name="x" /> .
public static decimal
MaxMagnitude​(decimal x, decimal y)
Compares two values to compute which is greater.
Returns <paramref name="x" /> if it is greater than <paramref name="y" /> ; otherwise, <paramref name="y" /> .
x The value to compare with <paramref name="y" /> .
y The value to compare with <paramref name="x" /> .
public static decimal
Min​(decimal x, decimal y)
Compares two values to compute which is lesser.
Returns <paramref name="x" /> if it is less than <paramref name="y" /> ; otherwise, <paramref name="y" /> .
x The value to compare with <paramref name="y" /> .
y The value to compare with <paramref name="x" /> .
public static decimal
MinMagnitude​(decimal x, decimal y)
Compares two values to compute which is lesser.
Returns <paramref name="x" /> if it is less than <paramref name="y" /> ; otherwise, <paramref name="y" /> .
x The value to compare with <paramref name="y" /> .
y The value to compare with <paramref name="x" /> .
public static decimal
Multiply​(decimal d1, decimal d2)
Multiplies two specified <see cref="T:System.Decimal" /> values.
Returns The result of multiplying <paramref name="d1" /> and <paramref name="d2" /> .
d1 The multiplicand.
d2 The multiplier.
public static decimal
Negate​(decimal d)
Returns the result of multiplying the specified <see cref="T:System.Decimal" /> value by negative one.
Returns A decimal number with the value of <paramref name="d" /> , but the opposite sign. -or- Zero, if <paramref name="d" /> is zero.
d The value to negate.
public static decimal
Parse​(ReadOnlySpan<​byte> utf8Text, NumberStyles style = 111, IFormatProvider? provider = null)
Parses a span of UTF-8 characters into a value.
Returns The result of parsing <paramref name="utf8Text" /> .
utf8Text The span of UTF-8 characters to parse.
style A bitwise combination of number styles that can be present in <paramref name="utf8Text" /> .
provider An object that provides culture-specific formatting information about <paramref name="utf8Text" /> .
public static decimal
Parse​(ReadOnlySpan<​byte> utf8Text, IFormatProvider? provider)
Parses a span of UTF-8 characters into a value.
Returns The result of parsing <paramref name="utf8Text" /> .
utf8Text The span of UTF-8 characters to parse.
provider An object that provides culture-specific formatting information about <paramref name="utf8Text" /> .
public static decimal
Parse​(ReadOnlySpan<​char> s, NumberStyles style = 111, IFormatProvider? provider = null)
Converts the span representation of a number to its <see cref="T:System.Decimal" /> equivalent using the specified style and culture-specific format.
Returns The <see cref="T:System.Decimal" /> number equivalent to the number contained in <paramref name="s" /> as specified by <paramref name="style" /> and <paramref name="provider" /> .
s The span containing the characters representing the number to convert.
style A bitwise combination of <see cref="T:System.Globalization.NumberStyles" /> values that indicates the style elements that can be present in <paramref name="s" /> . A typical value to specify is <see cref="F:System.Globalization.NumberStyles.Number" /> .
provider An <see cref="T:System.IFormatProvider" /> object that supplies culture-specific information about the format of <paramref name="s" /> .
public static decimal
Parse​(ReadOnlySpan<​char> s, IFormatProvider? provider)
Parses a span of characters into a value.
Returns The result of parsing <paramref name="s" /> .
s The span of characters to parse.
provider An object that provides culture-specific formatting information about <paramref name="s" /> .
public static decimal
Parse​(string s)
Converts the string representation of a number to its <see cref="T:System.Decimal" /> equivalent.
Returns The equivalent to the number contained in <paramref name="s" /> .
s The string representation of the number to convert.
public static decimal
Parse​(string s, NumberStyles style)
Converts the string representation of a number in a specified style to its <see cref="T:System.Decimal" /> equivalent.
Returns The <see cref="T:System.Decimal" /> number equivalent to the number contained in <paramref name="s" /> as specified by <paramref name="style" /> .
s The string representation of the number to convert.
style A bitwise combination of <see cref="T:System.Globalization.NumberStyles" /> values that indicates the style elements that can be present in <paramref name="s" /> . A typical value to specify is <see cref="F:System.Globalization.NumberStyles.Number" /> .
public static decimal
Parse​(string s, NumberStyles style, IFormatProvider? provider)
Converts the string representation of a number to its <see cref="T:System.Decimal" /> equivalent using the specified style and culture-specific format.
Returns The <see cref="T:System.Decimal" /> number equivalent to the number contained in <paramref name="s" /> as specified by <paramref name="style" /> and <paramref name="provider" /> .
s The string representation of the number to convert.
style A bitwise combination of <see cref="T:System.Globalization.NumberStyles" /> values that indicates the style elements that can be present in <paramref name="s" /> . A typical value to specify is <see cref="F:System.Globalization.NumberStyles.Number" /> .
provider An <see cref="T:System.IFormatProvider" /> object that supplies culture-specific information about the format of <paramref name="s" /> .
public static decimal
Parse​(string s, IFormatProvider? provider)
Converts the string representation of a number to its <see cref="T:System.Decimal" /> equivalent using the specified culture-specific format information.
Returns The <see cref="T:System.Decimal" /> number equivalent to the number contained in <paramref name="s" /> as specified by <paramref name="provider" /> .
s The string representation of the number to convert.
provider An <see cref="T:System.IFormatProvider" /> that supplies culture-specific parsing information about <paramref name="s" /> .
public static decimal
Remainder​(decimal d1, decimal d2)
Computes the remainder after dividing two <see cref="T:System.Decimal" /> values.
Returns The remainder after dividing <paramref name="d1" /> by <paramref name="d2" /> .
d1 The dividend.
d2 The divisor.
public static decimal
Round​(decimal d)
Rounds a decimal value to the nearest integer.
Returns The integer that is nearest to the <paramref name="d" /> parameter. If <paramref name="d" /> is halfway between two integers, one of which is even and the other odd, the even number is returned.
d A decimal number to round.
public static decimal
Round​(decimal d, int decimals)
Rounds a <see cref="T:System.Decimal" /> value to a specified number of decimal places.
Returns The decimal number equivalent to <paramref name="d" /> rounded to <paramref name="decimals" /> decimal places.
d A decimal number to round.
decimals A value from 0 to 28 that specifies the number of decimal places to round to.
public static decimal
Round​(decimal d, int decimals, MidpointRounding mode)
Rounds a decimal value to the specified precision using the specified rounding strategy.
Returns The number that <paramref name="d" /> is rounded to using the <paramref name="mode" /> rounding strategy and with a precision of <paramref name="decimals" /> . If the precision of <paramref name="d" /> is less than <paramref name="decimals" /> , <paramref name="d" /> is returned unchanged.
d A decimal number to round.
decimals The number of significant decimal places (precision) in the return value.
mode One of the enumeration values that specifies which rounding strategy to use.
public static decimal
Round​(decimal d, MidpointRounding mode)
Rounds a decimal value to an integer using the specified rounding strategy.
Returns The integer that <paramref name="d" /> is rounded to using the <paramref name="mode" /> rounding strategy.
d A decimal number to round.
mode One of the enumeration values that specifies which rounding strategy to use.
public static int
Sign​(decimal d)
Computes the sign of a value.
Returns A positive value if <paramref name="d" /> is positive, <see cref="P:System.Numerics.INumberBase`1.Zero" /> if <paramref name="d" /> is zero, and a negative value if <paramref name="d" /> is negative.
d The value whose sign is to be computed.
public static decimal
Subtract​(decimal d1, decimal d2)
Subtracts a specified <see cref="T:System.Decimal" /> value from another.
Returns The result of subtracting <paramref name="d2" /> from <paramref name="d1" /> .
d1 The minuend.
d2 The subtrahend.
public static byte
ToByte​(decimal value)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 8-bit unsigned integer.
Returns An 8-bit unsigned integer equivalent to <paramref name="value" /> .
value The decimal number to convert.
public static double
ToDouble​(decimal d)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent double-precision floating-point number.
Returns A double-precision floating-point number equivalent to <paramref name="d" /> .
d The decimal number to convert.
public static short
ToInt16​(decimal value)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 16-bit signed integer.
Returns A 16-bit signed integer equivalent to <paramref name="value" /> .
value The decimal number to convert.
public static int
ToInt32​(decimal d)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 32-bit signed integer.
Returns A 32-bit signed integer equivalent to the value of <paramref name="d" /> .
d The decimal number to convert.
public static long
ToInt64​(decimal d)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 64-bit signed integer.
Returns A 64-bit signed integer equivalent to the value of <paramref name="d" /> .
d The decimal number to convert.
public static long
ToOACurrency​(decimal value)
Converts the specified <see cref="T:System.Decimal" /> value to the equivalent OLE Automation Currency value, which is contained in a 64-bit signed integer.
Returns A 64-bit signed integer that contains the OLE Automation equivalent of <paramref name="value" /> .
value The decimal number to convert.
public static sbyte
ToSByte​(decimal value)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 8-bit signed integer.
Returns An 8-bit signed integer equivalent to <paramref name="value" /> .
value The decimal number to convert.
public static Single
ToSingle​(decimal d)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent single-precision floating-point number.
Returns A single-precision floating-point number equivalent to the value of <paramref name="d" /> .
d The decimal number to convert.
public string
ToString​()
Converts the numeric value of this instance to its equivalent string representation.
Returns A string that represents the value of this instance.
public string
ToString​(IFormatProvider? provider)
Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
Returns The string representation of the value of this instance as specified by <paramref name="provider" /> .
provider An object that supplies culture-specific formatting information.
public string
ToString​(string format)
Converts the numeric value of this instance to its equivalent string representation, using the specified format.
Returns The string representation of the value of this instance as specified by <paramref name="format" /> .
format A standard or custom numeric format string.
public string
ToString​(string format, IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
Returns The string representation of the value of this instance as specified by <paramref name="format" /> and <paramref name="provider" /> .
format A numeric format string.
provider An object that supplies culture-specific formatting information.
public static ushort
ToUInt16​(decimal value)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 16-bit unsigned integer.
Returns A 16-bit unsigned integer equivalent to the value of <paramref name="value" /> .
value The decimal number to convert.
public static uint
ToUInt32​(decimal d)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 32-bit unsigned integer.
Returns A 32-bit unsigned integer equivalent to the value of <paramref name="d" /> .
d The decimal number to convert.
public static ulong
ToUInt64​(decimal d)
Converts the value of the specified <see cref="T:System.Decimal" /> to the equivalent 64-bit unsigned integer.
Returns A 64-bit unsigned integer equivalent to the value of <paramref name="d" /> .
d The decimal number to convert.
public static decimal
Truncate​(decimal d)
Returns the integral digits of the specified <see cref="T:System.Decimal" /> ; any fractional digits are discarded.
Returns The result of <paramref name="d" /> rounded toward zero, to the nearest whole number.
d The decimal number to truncate.
public bool
TryFormat​(Span<​byte> utf8Destination, Int32& bytesWritten, ReadOnlySpan<​char> format = null, IFormatProvider? provider = null)
public bool
TryFormat​(Span<​char> destination, Int32& charsWritten, ReadOnlySpan<​char> format = null, IFormatProvider? provider = null)
public static bool
TryGetBits​(decimal d, Span<​int> destination, Int32& valuesWritten)
public static bool
TryParse​(ReadOnlySpan<​byte> utf8Text, Decimal& result)
public static bool
TryParse​(ReadOnlySpan<​byte> utf8Text, NumberStyles style, IFormatProvider? provider, Decimal& result)
public static bool
TryParse​(ReadOnlySpan<​byte> utf8Text, IFormatProvider? provider, Decimal& result)
public static bool
TryParse​(ReadOnlySpan<​char> s, Decimal& result)
public static bool
TryParse​(ReadOnlySpan<​char> s, NumberStyles style, IFormatProvider? provider, Decimal& result)
public static bool
TryParse​(ReadOnlySpan<​char> s, IFormatProvider? provider, Decimal& result)
public static bool
TryParse​(string s, Decimal& result)
public static bool
TryParse​(string s, NumberStyles style, IFormatProvider provider, Decimal& result)
public static bool
TryParse​(string s, IFormatProvider provider, Decimal& result)
protected void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .