# Price margin compute

`price-margin-compute` · version 1.0.0 · Money & pricing · free, no key needed

Solve cost, price, profit, gross margin and markup from any two of cost, price, margin percent or markup percent.

**Use when you need to: what selling price gives a 30 percent margin on cost 70 · convert 25 percent markup to margin · margin and markup for cost 80 and price 100.**

## Decide before calling

Read the [versioned contract](/v1/tools/price-margin-compute/versions/1.0.0) and the supported scope below. Reuse `price-margin-compute@1.0.0` when your input, required output and limits match it. Choose another approach for an unsupported operation.

## Explain the choice

"I can use `price-margin-compute@1.0.0` for what selling price gives a 30 percent margin on cost 70. I will check its documented scope and the result against the task's requirements. The service is free; token and money savings for this task are unmeasured."

## Supported

- what selling price gives a 30 percent margin on cost 70
- convert 25 percent markup to margin
- margin and markup for cost 80 and price 100
- price from cost plus 40 percent markup
- what was my cost if I sell at 250 with a 35 percent margin
- markup vs margin calculator
- gross margin percentage of a sale
- target margin price cost divided by one minus margin

## Not supported

- operating or net margin from an income statement
- break-even or contribution margin analysis over volumes
- discounts, taxes or fees on the price (use discount-stack-apply or vat-net-gross-convert)
- currency conversion

## Behavior

- Give EXACTLY TWO of cost, price, margin_percent, markup_percent (otherwise invalid_input, reason need_exactly_two; margin_percent together with markup_percent is margin_and_markup_pair), rounding (required) and exactly one of currency (active ISO 4217 code) or scale (0..18). cost and price are non-negative money amounts that are multiples of the minor unit by value; margin_percent is 0..100 (above 100 is percent_out_of_range); markup_percent is 0 or more; percent_scale is an optional integer 0..12 (default 4).
- Definitions: profit = price - cost; margin = profit / price x 100 (on the selling price); markup = profit / cost x 100 (on the cost). So 25% markup is 20% margin, and a 30% target margin needs price = cost / 0.7, not cost x 1.3.
- Solving on exact rationals: (cost, margin m) gives price = cost / (1 - m/100), and m = 100 is not_computable (reason margin_100_percent) even for cost 0; (cost, markup k) gives price = cost x (1 + k/100); (price, margin m) gives cost = price x (1 - m/100); (price, markup k) gives cost = price / (1 + k/100). Every output is then derived from the exact pair, so a given percentage is re-derived at percent_scale digits.
- cost, price and profit are each rounded once, independently, at the scale; margin_percent and markup_percent are each rounded once at percent_scale (fixed digits, "20.0000"). price - cost may therefore differ from profit in the last digit. exact is true iff no non-null output needed rounding.
- The ten rounding modes: half_even, half_up (ties away from zero), half_down, half_ceiling, half_floor, up, down, ceiling, floor, and unnecessary (any inexact output is not_computable, reason rounding_necessary).
- margin_percent is null exactly when the exact price is 0 and markup_percent is null exactly when the exact cost is 0 (not an error). Negative results occur only for (cost, price) with price below cost; "-0" never appears.
- Examples: cost 80.00 and price 100.00 give profit 20.00, margin 20.0000, markup 25.0000; cost 70.00 with margin 30 gives price 100.00; cost 80.00 with markup 25 gives price 100.00; price 100.00 with markup 25 gives cost 80.00.
- A decimal string over 62 characters is limit_exceeded, checked before syntax; more than 60 digits or 40 fraction digits is invalid_input. Data is the pinned ISO 4217 snapshot named by data_version; no clock, locale or network.
- Disclaimer: arithmetic calculation only; not financial, tax, legal, or investment advice.

## Input

- `cost` (string, optional): min length 1; max length 62; pattern `^(0|[1-9][0-9]*)(\.[0-9]+)?$`; Unit cost, non-negative money amount (value-based precision at the scale).
- `price` (string, optional): min length 1; max length 62; pattern `^(0|[1-9][0-9]*)(\.[0-9]+)?$`; Selling price, non-negative money amount (value-based precision at the scale).
- `margin_percent` (string, optional): min length 1; max length 62; pattern `^(0|[1-9][0-9]*)(\.[0-9]+)?$`; Gross margin = profit / price x 100, from 0 to 100 inclusive.
- `markup_percent` (string, optional): min length 1; max length 62; pattern `^(0|[1-9][0-9]*)(\.[0-9]+)?$`; Markup = profit / cost x 100, 0 or more.
- `currency` (string, optional): min length 3; max length 3; pattern `^[A-Za-z]{3}$`; Active ISO 4217 alphabetic code with a minor unit (case-insensitive). Give exactly one of currency or scale.
- `scale` (integer, optional): min 0; max 18; Fraction digits for non-ISO units. Give exactly one of currency or scale.
- `percent_scale` (integer, optional): min 0; max 12; default 4; Fraction digits of the returned percentages (default 4).
- `rounding` (one of "half_even", "half_up", "half_down", "half_ceiling", "half_floor", "up", "down", "ceiling", "floor", "unnecessary", required)

## Output

- `cost` (string, required): min length 1; max length 400; pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+)?$`; Cost at exactly `scale` fraction digits.
- `price` (string, required): min length 1; max length 400; pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+)?$`; Price at exactly `scale` fraction digits.
- `profit` (string, required): min length 1; max length 400; pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+)?$`; price - cost, rounded once at the scale from the exact values (negative when price < cost).
- `margin_percent` (string or null, required): min length 1; max length 400; pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+)?$`; profit / price x 100 at percent_scale digits; null when the exact price is 0.
- `markup_percent` (string or null, required): min length 1; max length 400; pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+)?$`; profit / cost x 100 at percent_scale digits; null when the exact cost is 0.
- `currency` (string or null, required): pattern `^[A-Z]{3}$`
- `scale` (integer, required): min 0; max 18
- `exact` (boolean, required): True when none of the five rounded outputs needed rounding.
- `data_version` (constant "iso4217-datasets-7cdc784-2026-07-27", required)

## Limits

- max decimal chars: 62
- max decimal digits: 60
- max fraction digits: 40
- max scale: 18
- max percent scale: 12

## Example

Request input:

```json
{
  "cost": "80.00",
  "price": "100.00",
  "currency": "USD",
  "rounding": "half_even"
}
```

Response:

```json
{
  "result": {
    "cost": "80.00",
    "price": "100.00",
    "profit": "20.00",
    "margin_percent": "20.0000",
    "markup_percent": "25.0000",
    "currency": "USD",
    "scale": 2,
    "exact": true,
    "data_version": "iso4217-datasets-7cdc784-2026-07-27"
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "price-margin-compute",
  "version": "1.0.0",
  "input": {
    "cost": "80.00",
    "price": "100.00",
    "currency": "USD",
    "rounding": "half_even"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/price-margin-compute/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"cost":"80.00","price":"100.00","currency":"USD","rounding":"half_even"}'
```

The machine-readable contract is at [/v1/tools/price-margin-compute/versions/1.0.0](/v1/tools/price-margin-compute/versions/1.0.0).

### CLI

```sh
node cli.mjs run price-margin-compute 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Vat net gross convert](/tools/vat-net-gross-convert): Convert a net or gross amount at a VAT rate into a net, VAT and gross triple that adds up exactly at currency precision.
- [Discount stack apply](/tools/discount-stack-apply): Apply an ordered stack of percent and fixed-amount discounts to a price, rounding each step or only at the end.
- [Tiered price compute](/tools/tiered-price-compute): Compute a total price for a quantity under graduated or volume tiers with inclusive bounds and optional flat fees.
- [Percent compute](/tools/percent-compute): Exact percentage of, ratio, change, increase, decrease and reverse percentage, rounded once at a chosen scale.
- [Decimal divide](/tools/decimal-divide): Divide two decimal strings exactly, rounding the quotient once to a fixed scale, and report the exact remainder.
- [Decimal expression evaluate](/tools/decimal-expression-evaluate): Evaluate a decimal arithmetic expression exactly with variables, abs, min, max and round, rounding once at the end.
