How we calculate

The formulas, number handling and rounding rules behind every PercentSwift result.

Exact decimals

Browsers store numbers in binary floating point, which can’t represent most decimal fractions exactly. That’s why a plain JavaScript calculation of 0.1 + 0.2 gives 0.30000000000000004. PercentSwift stores each number as a whole-number integer plus the position of the decimal point and does all addition, subtraction and multiplication exactly. Division is carried to 30 decimal places before rounding, which is far more precision than any displayed answer uses.

Rounding

Rounding happens once, at the end, so errors don’t pile up across steps. Halves round away from zero: 2.345 becomes 2.35 and −2.345 becomes −2.35.

On the Automatic setting, percentages and plain numbers show up to 6 decimal places with trailing zeros removed, and money amounts show exactly 2. When an answer has been shortened, the result says “Rounded to N decimal places”. You can choose a fixed number of places in Settings.

For money results that split a total into parts, such as a price and its tax, the parts are calculated from the rounded figures so that they add up to the total shown. When a bill is split between people, any leftover cent is pointed out rather than hidden.

Formulas

Mode Formula
X% of Y Y × X ÷ 100
X is what % of Y X ÷ Y × 100
X is P% of what X ÷ P × 100
Percentage change (new − old) ÷ |old| × 100
Percentage difference |A − B| ÷ ((A + B) ÷ 2) × 100
Add or subtract P% X × (1 ± P ÷ 100)
Value before a change final ÷ (1 ± P ÷ 100)
Successive changes start × (1 + c₁ ÷ 100) × (1 + c₂ ÷ 100) × …
Percentage points new % − old %, plus the relative change
Discount price × (1 − d ÷ 100), applied once per discount
Discount rate (price − sale) ÷ price × 100
Add tax or VAT amount × (1 + r ÷ 100)
Remove tax or VAT total ÷ (1 + r ÷ 100)
Tip bill × t ÷ 100, total ÷ people
Margin (price − cost) ÷ price × 100
Markup (price − cost) ÷ cost × 100
Price for a margin cost ÷ (1 − m ÷ 100)

Percentage change divides by the absolute value of the old number, so a move from −50 to −25 is reported as a 50% increase. This is a convention, and the result notes say so when the starting value is negative. A change from zero has no percentage, and the calculator says that instead of showing infinity or 0%.

Reading what you type

The calculator accepts numbers like 1,250.50, 1250.5, $1,250 and 12%. With the 1.234,56 number format chosen in Settings, it reads 1.250,50 the same way. If an input is ambiguous for the chosen format, such as 1,5 with the 1,234.56 format, the calculator asks instead of guessing. Inputs are limited to 24 digits before the decimal point and 12 after.

Testing

The arithmetic and every calculator mode are covered by automated tests with hand-worked expected answers, including rounding edges, large numbers, negative values and zero. Every calculator link inside the guides is also run through the same code each time the site is built, and the build stops if a stated answer doesn’t match.