Csharp/CSharp Tutorial/Data Type/decimal

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

decimal literal

<source lang="csharp">using System; class MainClass {

 static void Main(string[] args)
 {
   decimal MyDecimal = 3.50m;
   
   Console.WriteLine(MyDecimal);
 }

}</source>

3.50

Decimal ranges

<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {

   public static void Main()
   {
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(float).ToString(), sizeof(float), float.MinValue, float.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(double).ToString(), sizeof(double), double.MinValue, double.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(decimal).ToString(), sizeof(decimal), decimal.MinValue, decimal.MaxValue);
   }

}</source>

System.Single: bytes: 4, range: [-3.402823E+38,3.402823E+38]
System.Double: bytes: 8, range: [-1.79769313486232E+308,1.79769313486232E+308]
System.Decimal: bytes: 16, range: [-79228162514264337593543950335,79228162514264337593543950335]

decimals and arithmetic operators

<source lang="csharp">class MainClass {

 public static void Main()
 {
   
   System.Console.WriteLine("10m / 3m = " + 10m / 3m);
   decimal decimalValue1 = 10;
   decimal decimalValue2 = 3;
   System.Console.WriteLine("decimalValue1 / decimalValue2 = " + decimalValue1 / decimalValue2);
 }

}</source>

10m / 3m = 3.3333333333333333333333333333
decimalValue1 / decimalValue2 = 3.3333333333333333333333333333

decimal Types

<source lang="csharp">using System;

 class Class1
 {
   static void Main(string[] args)
   {
           decimal f;
           f = 999999999.123456789123456789123456789m;
           Console.WriteLine( "{0}", f );
   }
 }</source>

Declare decimal variable

  1. decimal is intended for use in monetary calculations.
  2. decimal utilizes 128 bits to represent values within the range 1E-28 to 7.9E+28.
  3. decimal eliminates the rounding errors caused by the normal floating-point data type.


<source lang="csharp">using System; using System.Collections.Generic; using System.Text; class MainClass {

   static void Main(string[] args)
   {
       decimal myDecimal = 150;
       double myDouble = 150;
   }

}</source>

Manually create a decimal number.

Decimal offers eight public constructors.

The following six are the most commonly used:

  1. public Decimal(int v)
  2. public Decimal(uint v)
  3. public Decimal(long v)
  4. public Decimal(ulong v)
  5. public Decimal(float v)
  6. public Decimal(double v)
  7. public Decimal(int low, int middle, int high, bool signFlag, byte scaleFactor)

You can specify the constituents of a Decimal in an array of integers, using this constructor:


<source lang="csharp">public Decimal(int[ ] parts)</source>

Specifying a Literal decimal

<source lang="csharp">class MainClass {

 static void Main()
 {
   System.Console.WriteLine(1.618m);
 }

}</source>

The Methods and Fields Defined by Decimal

Method Meaning public static decimal Add(decimal v1,decimal v2) Returns v1 + v2. public static int CompareTo(decimal v1,decimal v2) Returns zero if the values are equal. Returns a negative value if v1 is less than v2. Returns a positive value if v1 is greater than v2. public int CompareTo(object v) Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. public static decimal Divide(decimal v1,decimal v2) Returns v1 / v2. public override bool Equals(object v) Returns true if the value of the invoking object equals the value of v. public static bool Equals(decimal v1,decimal v2) Returns true if v1 equals v2. public static decimal Floor(decimal v) Returns the largest integer (represented as a decimal value) not greater than v. For example, given 1.02, Floor() returns 1.0. Given -1.02, Floor() returns -2. public static decimalFromOACurrency(long v) Converts the OLE Automation value in v into its decimal equivalent and returns the result. public static int[ ] GetBits(decimal v) Returns the binary representation of v and returns it in an array of ints. The organization of this array is as described in the text. public override int GetHashCode() Returns the hash code for the invoking object. public TypeCode GetTypeCode() Returns the TypeCode enumeration value for Decimal, which is TypeCode.Decimal. public static decimal Multiply(decimal v1,decimal v2) Returns v1 * v2. public static decimal Negate(decimal v) Returns -v. public static decimal Parse(string str) Returns the binary equivalent of the numeric string in str. public static decimalParse(string str,IFormatProvider fmtpvdr) Returns the binary equivalent of the numeric string in str using the culture-specific information provided by fmtpvdr. public static decimalParse(string str, NumberStyles styles) Returns the binary equivalent of the numeric string in str, using the style information provided by styles. public static decimalParse(string str,NumberStyles styles,IFormatProvider fmtpvdr) Returns the binary equivalent of the numeric string in str using the style information provided by styles and the culture-specific format information provided by fmtpvdr. public static decimalRemainder(decimal v1, decimal v2) Returns the remainder of the integer division v1 / v2. public static decimalRound(decimal v, int decPlaces) Returns the value of v rounded to the number of decimal places specified by decPlaces, which must be between 0 and 28. public static decimalSubtract(decimal v1, decimal v2) Returns v1 - v2. public static byte ToByte(decimal v) Returns the byte equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a byte. public static double ToDouble(decimal v) Returns the double equivalent of v. A loss of precision may occur because double has fewer significant digits than does decimal. public static short ToInt16(decimal v) Returns the short equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a short. public static int ToInt32(decimal v) Returns the int equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of an int. public static long ToInt64(decimal v) Returns the long equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a long. public static longToOACurrency(decimal v) Converts v into the equivalent OLE Automation value and returns the result. public static sbyte ToSByte(decimal v) Returns the sbyte equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of an sbyte. public static float ToSingle(decimal v) Returns the float equivalent of v. A loss of precision may occur because float has fewer significant digits than does decimal. public override string ToString() Returns the string representation of the value of the invoking object. public string ToString(string format) Returns the string representation of the value of the invoking object as specified by the format string passed in format. public stringToString(IFormatProvider fmtpvdr) Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr. public stringToString(string format,IFormatProvider fmtpvdr) Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr and the format specified by format. public static ushort ToUInt16(decimal v) Returns the ushort equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a ushort. public static uint ToUInt32(decimal v) Returns the uint equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a uint. public static ulong ToUInt64(decimal v) Returns the ulong equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a ulong. public static decimal Truncate(decimal v) Returns the whole-number portion of v. Thus, it truncates any fractional digits. Field Meaning public static readonly decimal MaxValue The largest value that a decimal can hold. public static readonly decimal MinusOne The decimal representation of -1. public static readonly decimal MinValue The smallest value that a decimal can hold. public static readonly decimal One The decimal representation of 1. public static readonly decimal Zero The decimal representation of 0.