Ternary / base-3 converter
42 is 1120 in ternary: one 27, one 9, two 3s and no units, shown step by step so you can check the arithmetic. Convert whole numbers, negative ones too, from decimal to base 3 and back. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Value
- 42
- From
- Decimal
- Input
- 42
- To
- ternary
- Base from
- 10
- Base to
- 3
A ternary base-3 converter turns whole numbers between everyday decimal notation and radix three, the place-value system that uses only digits zero, one, and two. Students learning positional notation, contest solvers working with ternary-coded puzzles, and engineers exploring balanced-ternary concepts all need the same deterministic path: no network, no random seeds, no wall-clock dependence.
What ternary (base 3) means and how it maps to decimal
Ternary is base three: each digit place is a power of three, and the only legal digits are zero, one, and two. The rightmost digit is three to the power zero, the next is three to the power one, then nine, twenty-seven, eighty-one, and so on; the decimal value is the sum of each digit times its place weight.
That is why the ternary string one-zero equals three in decimal, why two-two equals eight, and why one-one-two-zero equals one times twenty-seven plus one times nine plus two times three plus zero, which is forty-two. Decimal works the same way with powers of ten and digits zero through nine, so converting from decimal to ternary is repeated division by three, recording remainders, while converting from ternary to decimal is a left-to-right place-value accumulation.
Negative integers keep a leading minus on both forms; the magnitude is converted, then the sign is reattached so zero never becomes negative zero in the published strings. Ternary appears in programming contests, recreational math, and as the foundation for balanced ternary used in some theoretical computers—those systems still start from ordinary base-three place value with digits zero through two before remapping.
Converting by hand is error-prone past a few places; a deterministic path removes that risk while still showing the expansion for short inputs so learners can audit the arithmetic. If any character is not a legal digit for the declared source base after the optional sign is removed, conversion stops with an invalid digit error—there is no best-effort parsing that skips junk or invents a radix.
Practical tips, limits, and related searches
Always set from explicitly when a string could be read in both bases: the digits one-zero are three in ternary but ten in decimal, so auto-detection would be ambiguous and this tool never guesses. Prefer sending a string even when the value looks numeric; bare numbers are accepted only as finite whole numbers within the safe integer range as a convenience for small inputs. Do not embed spaces, commas, or underscore separators; strip them in your client before calling if your source uses grouped digits.
Sixty-four magnitude digits bound work without silent truncation; longer strings are rejected rather than cut. Prefer this capability when you already know the source is decimal or ternary and only need the other form; if you need hex or binary specifically, use those dedicated landings.
What you can do with it
Homework checks for base-3 place value
Students enter short decimal or ternary integers from worksheets and compare hand-written powers of three and the final conversion against a deterministic result before submitting.
Programming contest and puzzle solvers
Contest pipelines normalize ternary-coded problem inputs or expected outputs without reimplementing repeated division by three in every language.
FAQ
What is ternary or base 3?
Ternary is the place-value system with radix three. Digits are only 0, 1, and 2; each place is a power of three (1, 3, 9, 27, …). The decimal value is the sum of digit × 3^position.
Can I convert both directions?
Yes. Set from to decimal (default) to get ternary from a base-10 integer, or from to ternary to get decimal from a base-3 digit string.
What happens with non-numeric input?
Letters, spaces, commas, underscores, fractions, scientific notation, and digits outside the source alphabet are all rejected.
Is this balanced ternary?
No. This tool uses standard ternary digits 0, 1, and 2. Balanced ternary (digits −1, 0, +1) is a related system used in some theory and puzzles but is not encoded here.
Is the ternary converter free?
The browser tool is free and runs locally.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/enc/base-ternary \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"value":"42"}'const res = await fetch("https://api.kit.forhosting.com/enc/base-ternary", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"value": "42"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/enc/base-ternary",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"value": "42"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/enc/base-ternary", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"value":"42"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"value":"42"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/enc/base-ternary", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"value": "42"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "enc.base_ternary",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
max_digits | 64 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Non-numeric input, digits outside the source alphabet, separators, fractions, and scientific notation raise a clear invalid_input error instead of a silent wrong answer.
How to call the API and what comes back
Set from to decimal, base10, or ten when value is a base-ten integer; set from to ternary, base3, or three when value is a base-three digit string. After an optional leading plus or minus, only the alphabet for that source is allowed: zero through nine for decimal, and zero, one, or two for ternary. Send a JSON body with one required string field value (aliases n, input, text, and number) and an optional from field that defaults to decimal. Absolute length is capped at sixty-four magnitude digits so Worker and browser responses stay bounded. On success the response echoes the trimmed input, sets from and to to the source and target base names, base_from and base_to to ten and three or three and ten depending on direction, signed and negative flags for sign handling, decimal to the integer as a JSON number when it fits Number.MAX_SAFE_INTEGER or as a decimal digit string when it does not, decimal_string always to the full signed decimal representation, ternary and ternary_unsigned to the signed and absolute base-three strings, digit_count to the number of magnitude digits after stripping redundant leading zeros, message to a one-line English summary, and steps to an ordered list that records sign handling, validation, place-value or division steps, and the final result. Failed validation—missing value, empty digits after a bare sign, spaces or underscores, characters outside the source alphabet, fractions or scientific notation, or more than sixty-four digits—returns invalid_input and is never charged. Pricing is a flat $0.002 per successful request with unit item and zero marginal unit price.
Practical tips, limits, and related searches
For magnitudes larger than Number.MAX_SAFE_INTEGER the decimal field may be a string so no mantissa rounding can corrupt low bits—always prefer decimal_string when you need a stable textual form across languages. When teaching, pair examples: decimal forty-two maps to ternary one-one-two-zero, ternary two-zero-one maps to decimal nineteen, and a letter or a digit three in a ternary input yields invalid_input so students see the error path. This landing targets people searching for a ternary converter, base-3 calculator, decimal to ternary, or ternary to decimal tool; it is not a balanced-ternary encoder with minus-one digits, not a hex converter, and not a general arbitrary-base calculator with every radix from two to thirty-six. Failed tasks that return invalid_input do not consume prepaid balance, which keeps exploratory scripts cheap when you fuzz invalid digits. Round-trip checks are easy: convert a known decimal to ternary, then convert the ternary string back with from set to ternary, and confirm that decimal_string matches the original integer, then confirm that changing one character to an illegal digit flips the response to invalid_input with a message that names the bad digit.
Integration use cases
Unit tests for radix conversion libraries
Generate known-good decimal↔ternary pairs plus deliberate non-numeric and out-of-alphabet cases so coding kata expected values stay stable across runtimes.