Site icon JD Bots

How to Increment a Decimal Number in Valkey (INCRBYFLOAT)

Featured image for How to Increment a Decimal Number in Valkey, showing temperature set to 20.5, INCRBYFLOAT 1.25 replying 21.75 and INCRBYFLOAT minus 3 replying 18.75.

Valkey INCRBYFLOAT.

INCRBYFLOAT adds a decimal number, such as 1.25, to a value in Valkey®. A negative number subtracts. This guide changes a temperature reading.

Type the commands at the valkey-cli prompt. To open it, run valkey-cli in a terminal.

Note: Every command and output on this page is real, run on Valkey 9.2 on an Apple silicon Mac with macOS 26.

Syntax

Syntax

INCRBYFLOAT key increment

From the INCRBYFLOAT documentation.

Step 1: Save a Starting Value

127.0.0.1:6379>

SET temperature 20.5

Output

OK

Step 2: Add a Decimal

127.0.0.1:6379>

INCRBYFLOAT temperature 1.25

Output

"21.75"

The reply is the new value, in double quotes, because Valkey saves the result as text.

Step 3: Subtract with a Negative Number

127.0.0.1:6379>

INCRBYFLOAT temperature -3

Output

"18.75"

127.0.0.1:6379>

GET temperature

Output

"18.75"

Note: Not every decimal comes back this short. On a Mac with Apple silicon, adding 9.99 replies 9.99000000000000021: Fixed: Valkey INCRBYFLOAT Returns 9.99000000000000021 Instead of 9.99 on macOS explains why, and how to keep money exact.

Adding and subtracting decimals with INCRBYFLOAT.

Clean Up

127.0.0.1:6379>

DEL temperature

Output

(integer) 1

Common Mistakes

MistakeWhat happensDo this instead
Adding prices such as 9.99On a Mac the reply can show extra digitsCount whole cents with INCRBY
Looking for a DECRBYFLOAT commandThere isn’t onePass a negative number to INCRBYFLOAT

FAQ

Can INCRBYFLOAT work on a whole number?

Yes. 20 and 20.5 both work, and the result drops any zeros at the end, so 21.0 comes back as 21, as the documentation explains.

Does the key keep its expiry?

Yes. INCRBYFLOAT changes only the value.

Conclusion

INCRBYFLOAT key increment adds a decimal and replies with the new value. Use a negative number to subtract.

Related

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.

Exit mobile version