https://gamedev.stackexchange.com/a/199172/24266
I just spent a while writing some code for my 2D game to do this that should work correctly with negative coordinates. I informally checked it against Minecraft's F3 debug menu's chunk offsets, as well as wrote a few simple unit tests that I've provided below.
If anyone knows of a way to simplify or optimize this algorithm, do please comment and let me know!
// Copyright 2022 by Dan Bechard
// Your choice of any the following licenses:
// - Public domain