Decimal — GraphQL Custom Scalar

Author – ChilliCream

Date – 2025-12-29

1Overview

The Decimal scalar type represents a decimal floating-point number with high precision. It is intended for scenarios where precise decimal representation is critical, such as financial calculations, monetary values, scientific measurements, or any domain where floating-point rounding errors are unacceptable.

Unlike the built-in Float scalar which uses binary floating-point representation (IEEE 754) and can introduce rounding errors in decimal calculations, Decimal provides exact decimal representation.

Note: JavaScript’s number type uses binary floating-point (IEEE 754 double precision) and cannot accurately represent all decimal values. For example, 0.1 + 0.2 in JavaScript does not equal 0.3 exactly. Client applications using JavaScript should handle Decimal values with care, potentially using libraries like decimal.js, big.js, or bignumber.js for precise decimal arithmetic.

3Result spec

A Decimal scalar must serialize to a numeric value representing the decimal number.

The serialized value is a standard numeric representation that can include:

3.1Examples

These are valid result values:

Value Explanation
123.45 A positive decimal number.
-123.45 A negative decimal number.
0.123456789012345678901234567890 High-precision decimal.
1000000 Integer value as decimal.
0 Zero.

These are invalid result values:

Value Why is it invalid
"123.45" Must be a number, not a string.
NaN Not a valid number.
Infinity Not a valid decimal value.

4Input spec

A Decimal scalar accepts numeric values representing decimal numbers, both as GraphQL literals and as JSON input values.

Implementations should validate:

4.1Examples

Valid input values:

GraphQL Literal:

mutation {
  processPayment(amount: 99.99, taxRate: 0.08) {
    total
  }
}

JSON input:

{
  "amount": 99.99
}
{
  "price": 1234567890.123456789
}

Invalid input values:

Value Why is it invalid
NaN Not a valid number.
Infinity Not a valid decimal value.
"99.99" Must be a number, not a string.

5References

  1. 1Overview
  2. 2Recommended name
  3. 3Result spec
    1. 3.1Examples
  4. 4Input spec
    1. 4.1Examples
  5. 5References