A Simple Tip for Calculating Percentages

Document Type: Tip
Released: 2024-09-04

A Simple Tip

This tip won't win any awards, usher in world peace, or even spare you from hours of tedium.

But it does leverage some subtle details about data-type conversions, and can make calculating percentages with integers a bit easier - and more precise.

To get a sense for what I'm going on about, take a look at Figure 1, below:

Basic Percentage Calculation - against canned values.
FIGURE 1: Moving the 100 'up top' requires less conversion (and yields better precision).

On Line #3, you can see the normal, documented, and expected outcome of attempting to divide smaller integers by each other.

On Line #5, I've used some simple short-hand to convert my integers to decimal (or numeric) data-types by merely 'plunking' a decimal 'place-holder' on to the end of each of the values from Line #3. What's interesting, though, is that the results show a loss of precision.

Then, on Line #7, all that's happening is that I'm showing that you can use the formula partial_value * 100.0 / total_value as a means of obtaining a percentage vs the typical or conventional approach which is to use (partial_value / total_value) * 100.

Note too, that - interestingly enough - we've regained some precision with this approach - simply because we've got a larger dividend (i.e., partial * 100.0 is a much larger number to divide than what you get when dividing partial \ total).

Finally, a snapshot of my entire 'tip' is found on Line #9 - where I revert both the partial and total 'parts' of the perentage back into to their 'natural' integer types, and manage to still get a fully-precise and 100% viable percentage by MERELY a) using my partial * 100.0 / total 'formula' and by b) making sure that the 100.0 is of numeric or decimal data-type (which, in turn, coerces the other values in this equation to be of the same data type).

Again, this won't win any awards, or even save you gobs of time.

But, when it's used against actual TABLE data (vs static (pre-known) values), this tip can save you a decent amount of cognitive clutter by decreasing the number CAST/CONVERT operations you'd otherwise need to employ - as can be seen from the following screenshot:

Examples of the tip in action.
FIGURE 2: The code highlighted in green is simpler to read (and type) AND yields better precision.

Or, feel free to take the code for a spin yourself:


DECLARE @integers table ( 
	partial_value int NOT NULL, 
	total_value int NOT NULL 
); 

INSERT INTO @integers ([partial_value], [total_value])
VALUES
(10, 100), 
(3, 9), 
(54, 267);

SELECT 
	CAST([partial_value] AS decimal(10,2)) 
	/ 
	CAST([total_value] AS decimal(10,2)) * 100.0 [traditional]
FROM 
	@integers;

SELECT 
	[partial_value] * 100.0 / [total_value] [simpler]
FROM 
	@integers;
	

Either way, you'll see that partial_value * 100. / total_value is a simple trick that decreases the need for CAST()/CONVERT() operations, while even frequently yielding BETTER precision.