Schien Dong came across this function that calculates the spacing of numbers on the Y-axis of a charting component. It works, but in a very mysterious way ....

CHALLENGE POINT: Post a better (or worse) way of accomplishing this roundabout way of rounding.

private int CalculateGap(int min, int max, int interval)
{
	int gap = (int)((max - min) / interval);

	string gapString = gap.ToString();
	int length = gapString.Length;

	// Use the first (and possibly second and third) digit of the
	// calculated gap to come up with a reasonable interval
	// value to display.
	int result = 0;
	int firstDigit = 0;
	int secondDigit = 0;
	int thirdDigit = 0;

	if (length > 0)
	{
		firstDigit = int.Parse(gapString.Substring(0, 1));
	}   

	if (length > 1)
	{
		secondDigit = int.Parse(gapString.Substring(1, 1));
	}

	if (length > 2)
	{
		thirdDigit = int.Parse(gapString.Substring(2, 1));
	}

	if (thirdDigit >= 5)
	{
		// Round up
		secondDigit++;
	}

	if (secondDigit >= 5)
	{
		// Round up
		firstDigit++;
	}

	result = firstDigit * (int)Math.Pow(10, length - 1);

	return result;
}

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!