Skip to main content
0 reactions

Fixed: Valkey INCRBYFLOAT Returns 9.99000000000000021 Instead of 9.99 on macOS

Featured image for the fix of INCRBYFLOAT showing 9.99000000000000021 on a Mac, with a warning sign and the fix, INCRBY in whole cents.
Fixed: INCRBYFLOAT shows 9.99000000000000021 on a Mac.

If INCRBYFLOAT in Valkey® on a Mac replies with a number like this when you added 9.99, nothing is broken. The extra digits come from how Macs with Apple silicon store decimal numbers, and whole cents avoid them.

OutputPlain text
"9.99000000000000021"

What I Was Doing

I was writing Valkey Tutorial — Part 4: Strings and Counters on a Mac with Apple silicon and macOS 26, running Valkey 9.2, and adding up a bookshop’s takings with INCRBYFLOAT: a sale of 9.99, then one of 22.50.

What I Expected

9.99, then 32.49. Valkey’s INCRBYFLOAT documentation (opens in a new tab) shows short results, such as 10.6.

What I Got

127.0.0.1:6379>Plain text
INCRBYFLOAT revenue:today 9.99
OutputPlain text
"9.99000000000000021"
127.0.0.1:6379>Plain text
INCRBYFLOAT revenue:today 22.5
OutputPlain text
"32.49000000000000199"
Terminal window where INCRBYFLOAT revenue:today 9.99 replies 9.99000000000000021, and adding 22.5 replies 32.49000000000000199.
INCRBYFLOAT on a Mac with Apple silicon shows extra digits.

What I Checked

I ran the example from the INCRBYFLOAT documentation on the same Mac. The documentation shows "10.6":

127.0.0.1:6379>Plain text
SET mykey 10.50
OutputPlain text
OK
127.0.0.1:6379>Plain text
INCRBYFLOAT mykey 0.1
OutputPlain text
"10.59999999999999964"

Valkey’s own example gives a longer number here too, so the digits come from the computer, not from my values. Two official sources explain it:

Why It Happens

A computer stores decimal numbers in binary, and most of them, like 9.99, can’t be stored exactly: the stored value is off by a tiny amount. On a Mac with Apple silicon, Valkey works with 64-bit numbers, and printing 17 digits after the point is enough to show that tiny difference: 9.99 comes back as 9.99000000000000021.

Valkey saves the same text it replies with, so the extra digits stay in the key and travel into every later total.

The Fix

Count Money in Whole Cents with INCRBY

Store amounts as whole numbers of cents. INCRBY works on whole numbers, which Valkey keeps exactly, as its INCR documentation (opens in a new tab) explains. Use a new key, because INCRBY refuses a key that holds a decimal:

127.0.0.1:6379>Plain text
INCRBY revenue:today 1
OutputPlain text
(error) ERR value is not an integer or out of range
127.0.0.1:6379>Plain text
INCRBY revenue:today:cents 999
OutputPlain text
(integer) 999
127.0.0.1:6379>Plain text
INCRBY revenue:today:cents 2250
OutputPlain text
(integer) 3249
127.0.0.1:6379>Plain text
GET revenue:today:cents
OutputPlain text
"3249"

3249 cents is 32.49. Your app divides by 100 only when it shows the amount.

Or Round the Number When You Show It

If you keep INCRBYFLOAT, round the result to the decimals you need when your app reads it. The value in Valkey keeps the extra digits.

After the Fix

Terminal window where INCRBY revenue:today:cents 999 replies 999, adding 2250 replies 3249, and GET returns 3249.
Whole cents with INCRBY: 3249, with no extra digits.

The total is a whole number of cents, with no extra digits.

Common Mistakes

MistakeWhat happensDo this instead
Comparing an INCRBYFLOAT result with an exact amount such as 32.49The comparison fails, because the stored value is 32.49000000000000199Count whole cents with INCRBY
Switching to INCRBY on the old keyIt refuses the decimal already stored thereStart a new key, such as revenue:today:cents

Troubleshooting

ProblemCauseFix
"9.99000000000000021" or "10.59999999999999964"INCRBYFLOAT on a Mac with Apple siliconCount whole cents with INCRBY, or round when you show the value
(error) ERR value is not an integer or out of rangeThe key holds a decimal from INCRBYFLOATUse a new key for the whole-cent count

FAQ

Is the stored amount wrong?

Only by about 0.0000000000000002, far less than a cent. But it’s saved in the key, so totals carry it along and exact comparisons fail.

Does INCRBY have the same problem?

No. INCRBY works on 64-bit whole numbers, which binary stores exactly.

Conclusion

On a Mac with Apple silicon, INCRBYFLOAT replies with 17 digits after the point, which shows the tiny rounding of numbers like 9.99. Count money in whole cents with INCRBY, and divide by 100 only when you show it.

Additional Resources

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

0 comments