# Decimal data type

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Decimal_data_type
> Markdown URL: https://mediated.wiki/source/Decimal_data_type.md
> Source: https://en.wikipedia.org/wiki/Decimal_data_type
> Source revision: 1352638493
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Data type for storing floating-point numbers in base-10

Some [programming languages](/source/Programming_languages) (or compilers for them) provide a built-in (primitive) or library **decimal [data type](/source/Data_type)** to represent non-repeating [decimal fractions](/source/Decimal_fraction) like 0.3 and −1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal or num7.Num type of [Python](/source/Python_(programming_language)), and analogous types provided by other languages.

## Rationale

Fractional numbers are supported on most programming languages as [floating-point numbers](/source/Floating-point_number) or [fixed-point numbers](/source/Fixed-point_number). However, such representations typically restrict the denominator to a power of two. Most decimal fractions (or most fractions in general) cannot be represented exactly as a fraction with a denominator that is a power of two. For example, the simple decimal fraction 0.3 (⁠3/10⁠) might be represented as ⁠5404319552844595/18014398509481984⁠ (0.299999999999999988897769…). This inexactness causes many problems that are familiar to experienced programmers. For example, the expression 0.1 * 7 == 0.7 might counterintuitively evaluate to false in some systems, due to the inexactness of the representation of decimals.

Although all decimal fractions are [fractions](/source/Fraction_(mathematics)), and thus it is possible to use a [rational data type](/source/Rational_data_type) to represent it exactly, it may be more convenient in many situations to consider only non-repeating decimal fractions (fractions whose denominator is a power of ten). For example, fractional units of currency worldwide are mostly based on a denominator that is a power of ten. Also, most fractional measurements in science are reported as decimal fractions, as opposed to fractions with any other system of denominators.

A decimal data type could be implemented as either a [floating-point number](/source/Floating-point_number) or as a [fixed-point number](/source/Fixed-point_number). In the fixed-point case, the denominator would be set to a fixed power of ten. In the floating-point case, a variable exponent would represent the power of ten to which the mantissa of the number is multiplied.

Languages that support a rational data type usually allow the construction of such a value from two integers, instead of a base-2 floating-point number, due to the loss of exactness the latter would cause. Usually the basic arithmetic operations (+, −, ×, ÷, integer [powers](/source/Power_(mathematics))) and comparisons (=, <, >, ≤, ≥, ≠) would be extended to act on them—either natively or through [operator overloading](/source/Operator_overloading) facilities provided by the language. These operations may be translated by the [compiler](/source/Compiler) into a sequence of integer [machine instructions](/source/Machine_instruction), or into [library](/source/Library_(computer_science)) calls. Support may also extend to other operations, such as formatting, rounding to an integer or [floating point](/source/Floating_point) value, etc.

## Standard formats

[IEEE 754](/source/IEEE_754) specifies three standard floating-point decimal data types of different precision, introduced in [the 2008 revision](/source/IEEE_754-2008_revision) of the standard (also adopted as ISO/IEC/IEEE 60559:2011):

- [Decimal32 floating-point format](/source/Decimal32_floating-point_format)

- [Decimal64 floating-point format](/source/Decimal64_floating-point_format)

- [Decimal128 floating-point format](/source/Decimal128_floating-point_format)

## Language support

- [C#](/source/C_Sharp_(programming_language)) has a built-in data type decimal consisting of 128 bits resulting in 28–29 significant digits. It has an approximate range of ±1.0 × 10−28 to ±7.9228 × 1028.[1]

- Starting with Python 2.4, [Python](/source/Python_(programming_language))'s standard library includes a Decimal class in the module decimal.[2]

- [Ruby](/source/Ruby_(programming_language))'s standard library includes a BigDecimal class in the module bigdecimal.

- [Java](/source/Java_(programming_language))'s standard library includes a [java.math.BigDecimal](http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html) class.

- In [Objective-C](/source/Objective-C), the [Cocoa](/source/Cocoa_(API)) and [GNUstep](/source/GNUstep) [APIs](/source/API) provide an [NSDecimalNumber](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html) class and an [NSDecimal](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html) C data type for representing decimals whose mantissa is up to 38 digits long, and exponent is from −128 to 127.

- Some IBM systems and SQL systems support DECFLOAT format with at least the two larger formats.[3]

- ABAP's new DECFLOAT data type includes decimal64 (as DECFLOAT16) and decimal128 (as DECFLOAT34) formats.[4]

- [PL/I](/source/PL%2FI) natively supports both fixed-point and floating-point decimal data.

- [GNU Compiler Collection](/source/GNU_Compiler_Collection) (gcc) provides support for decimal floats as an extension to C and C++.[5]

## See also

- [Arbitrary-precision arithmetic](/source/Arbitrary-precision_arithmetic)

- [Floating-point arithmetic](/source/Floating-point_arithmetic)

- [Floating-point error mitigation](/source/Floating-point_error_mitigation)

## References

1. **[^](#cite_ref-1)** ["Floating-point numeric types - C# reference"](http://msdn.microsoft.com/en-us/library/364x0z75.aspx). 29 September 2022.

1. **[^](#cite_ref-2)** ["Decimal — Decimal fixed point and floating point arithmetic — Python 3.10.0 documentation"](https://docs.python.org/library/decimal.html).

1. **[^](#cite_ref-3)** ["Data management"](http://www.ibm.com/developerworks/data/library/techarticle/dm-0801chainani/). *[IBM](/source/IBM)*.

1. **[^](#cite_ref-4)** ["How the new ABAP Data Type DECFLOAT helps computing complex calculation? « SAP Ignite"](https://web.archive.org/web/20120809062259/http://sapignite.com/decfloat-in-abap/). Archived from [the original](http://sapignite.com/decfloat-in-abap/) on 2012-08-09. Retrieved 2012-07-28.

1. **[^](#cite_ref-5)** ["GCC Manual"](https://gcc.gnu.org/onlinedocs/gcc/Decimal-Float.html#Decimal-Float). 6.13 Decimal Floating Types.

v t e Data types Uninterpreted Bit Byte Trit Tryte Word Bit array Numeric Arbitrary-precision or bignum Complex Decimal Fixed point Block floating point Floating point Reduced precision Minifloat Half precision bfloat16 Single precision Double precision Quadruple precision Octuple precision Extended precision Long double Integer signedness Interval Rational Reference Address physical virtual Pointer Text Character String null-terminated Composite Algebraic data type generalized Array Associative array Class Dependent Equality Inductive Intersection List Object metaobject Option type Product Record or Struct Refinement Set Union tagged Other Any type Boolean Bottom type Collection Enumerated type Exception Function type Opaque data type Recursive data type Semaphore Stream Strongly typed identifier Type class Empty type Unit type Void Related topics Value Abstract data type Boxing Data structure Generic Kind metaclass Parametric polymorphism Primitive data type Interface Subtyping Type constructor Type conversion Type system Type theory Variable

---
Adapted from the Wikipedia article [Decimal data type](https://en.wikipedia.org/wiki/Decimal_data_type) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Decimal_data_type?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
