Differences between decimal and double in C # data types

Source: Internet
Author: User

Decimal type

As a supplement, the decimal type is used to represent high precision floating-point numbers

Name
CTS Type
De script ion
Significant figures
Range (approximate)
Decimal
System.Decimal
128-bit High Precision decimal notation
28
±1.0x10?28 to±7.9x1028

As you can see from the table above, decimal has a large number of significant digits, reaching 28 digits, but the range of data represented is smaller than the float and double types. The decimal type is not the underlying type in C #, so it can be used to affect performance at the time of calculation.

Decimal 128bit Size Valid number: 28~29, although the decimal type has a higher precision than a floating-point type, it has a smaller range. Therefore, double decimal is likely to overflow error, in addition, the calculation of decimal is slightly slower.

Console.WriteLine (1.618031234567895);

Output Result: 1.61803123456789

The last digit 5 was lost, which is in line with our expectation of double type precision.

To display numbers with full precision, you must declare the literal value to be of type Decimal, which is accomplished by appending an m (or m):

Console.WriteLine (1.618031234567895m);

Output Result: 1.618031234567895

The reason for decimal is expressed in m because this data type is often used in currency (monetary) calculations.

Decimal type.

Type
Approximate range
Precision
. NET Framework Types
Decimal
±1.0x10e?28 to ±7.9x10e28
28 to 29-bit valid bit
System.Decimal

s when declaring decimal type data, you can a:decimal MyData = 100, at which point the compiler implicitly converts the integer number 100 to 100.0m, and of course you can b:decimal MyData = 100.0m, but if it is decimal myData = 100.0d or Decimal myData = 100.0f, it is not, because 100.0d or 100.0f, the compiler considers floating-point numbers, and there is no implicit conversion between floating-point numbers and decimal types; Therefore, you must use casts to convert between the two types. This is the important, otherwise the compiler will complain. So the general financial software in the processing, will use the decimal type.
OK, when you switch to the decimal type, it's OK, and the result will be a full display of 66.24.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.