Gross margin is calculated by subtracting the cost of goods sold (COGS) from total revenue and dividing the result by total revenue. The formula for gross margin is: Gross Margin = (Total Revenue - COGS) / Total Revenue. This yields a percentage that represents the portion of revenue that remains after accounting for the costs of producing or delivering the product or service.