Snap vector direction to nearest axis

I have this case when I need to get my character direction is snapped to the nearest axis. Kinda look like this.

View post on

My solution is to find the largest axis absolute value of axis. Then, change the largest one to 1 and the others 2 with 0. Change largest axis sign with its original sign.

Example, we have Vector3(-0.5f, 0.1f, 0). The snapped direction is Vector3(-1,0,0).