Adding unit test for #39410. Implementing fix by switching from using Math.log to Integer.toString to figure out the number of digits.