Decimal to fraction
decimal-to-fraction · version 1.0.0 · Money & pricing · free, no key needed
Convert a decimal or repeating decimal like 0.41(6) to a reduced fraction, or the closest fraction under a cap.
Use when you need to: convert decimal to fraction · repeating decimal to fraction · 0.333 as a reduced fraction.
Decide before calling
Read the versioned contract and the supported scope below. Reuse decimal-to-fraction@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 decimal-to-fraction@1.0.0 for convert decimal to fraction. 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
- convert decimal to fraction
- repeating decimal to fraction
- 0.333 as a reduced fraction
- closest fraction with denominator at most 100
- best rational approximation of pi
- simplify a decimal into lowest terms
- approximate a decimal with a small denominator
- exact fraction for 0.(142857)
Not supported
- converting fractions into decimal expansions
- irrational numbers, exponent notation or symbolic constants
- percent or mixed number formatting
- continued fraction expansion listing
Behavior
- value is either a plain decimal ^-?(0|[1-9][0-9]*)(\.[0-9]+)?$ or a repeating decimal ^-?(0|[1-9][0-9]*)\.([0-9]*)\(([0-9]+)\)$: an integer part, a point, an optional non-repeating fraction part (may be empty), and exactly one non-empty parenthesised repeating block that runs to the end of the string. "0.(142857)", "0.1(6)", "0.41(6)" and "12.(3)" are valid; "0.(3)(4)", "0.3(", "0.()", "(3)", ".5", "1.", "1e3", "0.(3)x" and "0,5" are invalid_input.
- Length limit: a value longer than 64 UTF-16 code units -> limit_exceeded, checked before the grammar. A value of up to 64 characters that violates the grammar or the digit limits -> invalid_input. The digit limits are: at most 60 digits in total (integer part, non-repeating part and block together) and at most 40 fraction digits (non-repeating part plus repeating block). A non-string value is invalid_input.
- The repeating block may start with zeros and may be "0" or "9": "0.(0)" is 0, "1.(0)" is 1, "0.(9)" is exactly 1 (numerator "1", denominator "1", exact true), "0.(003)" is 1/333, "0.0(3)" is 1/30. A negative sign applies to the whole value: "-0.1(6)" is -1/6.
- Exact conversion of a repeating decimal: value = integer + non_repeating / 10^a + block / (10^a * (10^b - 1)), with a and b the lengths of the two parts, then reduced to lowest terms. Verify: 0.41(6) = 41/100 + 6/900 = 5/12.
- Without max_denominator the result is the exact fraction in lowest terms and exact is true. The denominator is always positive; the sign is carried by the numerator. Zero is "0" over "1" with no sign ("-0", "-0.00" and "0.(0)" all give 0/1).
- max_denominator is a decimal integer string matching ^[1-9][0-9]*$, value 1 to 10^18 (1000000000000000000). Anything else ("0", "01", "-3", "7.0", "1e3", a JSON number, 10^18+1, empty) is invalid_input. If the exact reduced denominator is <= N the exact fraction is returned; otherwise the result is the fraction closest to the value among all fractions with denominator <= N.
- Closest fraction: minimise the absolute error |value - p/q| over all q <= N. On an exact tie between two candidates choose the one with the smaller denominator, and if the denominators are equal choose the one closer to zero (smaller magnitude). Examples: 0.41(6) = 5/12 with N=3 ties between 1/3 and 1/2 (both 1/12 away) and gives 1/2 (denominator 2 < 3, error "-1/12"); 0.5 with N=1 ties between 0/1 and 1/1 and gives 0/1; -1.5 with N=1 gives -1/1; 1.5 with N=1 gives 1/1. The result is in lowest terms; a zero numerator has denominator 1. Worked references (edge vectors, computed): Pi to 40 places with N=7 gives 22/7, N=100 gives 311/99, N=106 gives 333/106, N=113 gives 355/113 and N=33102 gives 103993/33102 (OEIS A002485/A002486 convergents; N=100 is the best of the semiconvergents, not a convergent).
- error is the exact signed difference value - returned fraction, reduced, written "n/d" with the sign on n ("-1/3000" when 0.333 is approximated by 1/3; "22/700" would be written "11/350"), or "0" when exact. It is never a rounded decimal.
- Validation order: input not an object; unknown field; missing value; field types; value length limit_exceeded; value grammar and digit limits; max_denominator range. There are no not_computable cases.
Input
value(string, required): min length 1; max length 64; pattern^-?(0|[1-9][0-9]*)(\.[0-9]*\([0-9]+\)|\.[0-9]+)?$; A decimal string, or a repeating decimal such as "0.41(6)" or "0.(142857)", with an optional leading minus sign.max_denominator(string, optional): min length 1; max length 19; pattern^[1-9][0-9]*$; Optional cap N as a decimal integer string, 1 to 1000000000000000000 (10^18). When given, the closest fraction with denominator <= N is returned.
Output
numerator(string, required): min length 1; max length 110; Numerator as an integer string; carries the sign; "0" for zero.denominator(string, required): min length 1; max length 41; Positive denominator as an integer string, in lowest terms.exact(boolean, required): true when the returned fraction equals the input exactly.error(string, required): min length 1; max length 260; Exact signed value input minus fraction as a reduced "n/d" string (sign on n), or "0" when exact.
Limits
- max value chars: 64
- max total digits: 60
- max fraction digits: 40
- max denominator cap: 10^18
Example
Request input:
{
"value": "0.41(6)"
}
Response:
{
"result": {
"numerator": "5",
"denominator": "12",
"exact": true,
"error": "0"
}
}
How to call it
MCP
Connect https://computefirst.net/mcp (setup), then call execute with:
{
"id": "decimal-to-fraction",
"version": "1.0.0",
"input": {
"value": "0.41(6)"
}
}
HTTP (no key)
curl -X POST https://computefirst.net/v1/tools/decimal-to-fraction/versions/1.0.0/execute \
-H "Content-Type: application/json" \
-d '{"value":"0.41(6)"}'
The machine-readable contract is at /v1/tools/decimal-to-fraction/versions/1.0.0.
CLI
node cli.mjs run decimal-to-fraction 1.0.0 --input input.json --base-url https://computefirst.net
Get the client at /clients/cli/.
Related tools
- Fraction to decimal expand: Expand an integer fraction into its exact decimal with the minimal repeating block marked, e.g. 1/7 as 0.(142857).
- 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.
- Decimal expression evaluate: Evaluate a decimal arithmetic expression exactly with variables, abs, min, max and round, rounding once at the end.
- Price margin compute: Solve cost, price, profit, gross margin and markup from any two of cost, price, margin percent or markup percent.
- Decimal divide: Divide two decimal strings exactly, rounding the quotient once to a fixed scale, and report the exact remainder.
- Decimal power: Raise a decimal string to an integer power exactly, with optional single rounding to a fixed scale.