🛠️ToolsShed

Margin & Markup Calculator

Calculate profit margin percentage and markup percentage from cost and price.

常见问题

代码实现

def margin_from_cost_price(cost: float, price: float) -> dict:
    """Calculate margin and markup from cost and selling price."""
    if price == 0:
        raise ValueError("Selling price cannot be zero")
    profit = price - cost
    margin = (profit / price) * 100
    markup = (profit / cost) * 100 if cost > 0 else float('inf')
    return {"profit": profit, "margin": margin, "markup": markup}

def price_from_cost_margin(cost: float, margin_pct: float) -> dict:
    """Calculate price from cost and desired margin percentage."""
    if margin_pct >= 100:
        raise ValueError("Margin must be less than 100%")
    price = cost / (1 - margin_pct / 100)
    profit = price - cost
    markup = (profit / cost) * 100 if cost > 0 else float('inf')
    return {"price": price, "profit": profit, "markup": markup}

def price_from_cost_markup(cost: float, markup_pct: float) -> dict:
    """Calculate price from cost and desired markup percentage."""
    profit = cost * (markup_pct / 100)
    price = cost + profit
    margin = (profit / price) * 100 if price > 0 else 0
    return {"price": price, "profit": profit, "margin": margin}

# Example
result = margin_from_cost_price(cost=50, price=80)
print(f"Margin: {result['margin']:.2f}%")
print(f"Markup: {result['markup']:.2f}%")
print(f"Profit: ${result['profit']:.2f}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.