Float field returns whole number when decimal is `.0`

Long question short, should I create a Decimal scalar? And if so is there a way to preserve it as an INT with trailing zeros instead of needing to convert it to a string?


There is a need from my consumers to always receive decimals even if the trailing values are 0.

For example, instead of receiving 10 they want to receive 10.0. This isn’t a schema-wide rule at the moment, it’s mostly specific to a handful of data-point fields in an isolated query. However, it’s not to say it won’t transcend the specific fields/query in the future. Here is additional info:

Schema:

type Query {
  getValues: Values
}

type Values {
  valueOne: Float
  valueTwo: Float
}

Query:

query GetValues {
  getValues {
    valueOne
    valueTwo
  }
}

Response:

{
  "data": {
    "getValues": {
      "valueOne": 10 // value returned from resolver is "10.0"
      "valueTwo": 10.4 // value returned from resolver is "10.4"
    }
  }
}

Resolver helper logic:

function applyValueDecimalPlaces(number: number): string {
  return number.toLocaleString('en-us', {
    maximumFractionDigits: 1,
    minimumFractionDigits: 1,
  });
}

applyValueDecimalPlaces(9.96) // returns "10.0"
applyValueDecimalPlaces(10.36) // returns "10.4"

So, the logic seems to work as expected all the way up to the resolver-schema hand off.. the resolver is providing 10.0 in this case but the response is 10. How do I ensure the consumer receives 10.0 and not 10?

do the clients need to do math on the result, or will “10.0” be display only? IOW, is this a number or a string?

1 Like

That’s a good question. We do have “l10n” fields that act as display-ready and localized strings.. those are returning “10.0 units” just fine, it’s just the number fields.

As for now, the consumers shouldn’t need to do math, the “raw data” (float) is used to populate a VisX chart library for visualizing the data.

After working with the consumers, we decided that 10 (Float) is an appropriate response for the field they will use for math / data config / etc. We have formatted fields that will be strings and return the expected display ready value of "10.0 units".