I’m using the following GraphQL query to retrieve the weight:
inventoryItem {
measurement {
weight {
unit
value
}
}
}
In the response, I noticed that the weight value with the lb (pound) unit is always rounded to two decimal places, even though the original data contains more decimal places.
How can I retrieve the weight value with its full precision?
“I think this is currently not possible because Shopify defines the data type for this field as Float.”
Worth checking whether the precision is being lost on write rather than on read. Set a weight with more decimals through inventoryItemUpdate, then read it straight back in the same unit.
If it already comes back at two places, the stored value is rounded and Float isn’t the limiting factor, which would leave a metafield as the only way to keep the original figure.
Hey @NguyenTung, thanks for sharing this.
The float scalar is able to store more than two decimal places. So the issue isn’t at that level.
I was able to replicate what you were doing, leaning in to the suggestion by @Mubashir_Hassan. When I use pounds I do see it rounding to two decimal places the same as you are. I’m looking into this now to see if this is expected. I will follow up here with whatever I find.
Hey everyone, I have an update here. Currently, rounding to two decimal places is expected.
That said, we are looking into this further to see if this is a bug or not (since the API schema references this as a float).
I’ll do my best to update here once we have investigated that further (no timeline at this point). For now, assuming two decimal places is the best workaround.
Thanks @KyleG-Shopify for the feedback. I’d like to add one more point: the number of decimal places that gets rounded appears to depend on the weight unit. Specifically, I’ve observed the following:
- g: GraphQL returns all 4 decimal places, matching the value stored by Shopify.
- kg: rounded to 3 decimal places.
- lb: rounded to 2 decimal places.
- oz: rounded to 1 decimal place.
I don’t understand the actual reason behind this behavior. It can cause discrepancies in weight-related calculations, for example when calculating shipping costs. For instance, 50 units × 0.01 lb can produce a very different result from 50 units × the actual value of 0.014 lb.
I’m an app developer, so I can’t reasonably ask merchants to store the weight in a metafield just to work around this limitation. That would seem quite unreasonable, especially when the more precise value already exists in Shopify.
That lines up with Shopify storing weight internally in grams. Converting an integer gram value to kg/lb/oz naturally caps at a different decimal place per unit, which would explain why the rounding is unit-specific rather than a Float limitation.
Yes, those are the decimal places that I’m getting when I test as well.
Those are very fair points on how you do need this precision. Thanks for that additional clarity as well.