DecimalToFraction
DecimalToFraction
DecimalToFraction[ d ] returns the simplest fraction with the same value as a decimal number.
Usages
DecimalToFraction[ d ]
DecimalToFraction[ d ] writes the decimal number d as a fraction with exactly the same value, simplified as far as possible. d can have at most 10 decimals.
| Example | in | out |
|---|---|---|
1.25 = 5/4. |
DecimalToFraction[ 1.25 ] |
5/4 |
| A negative number. | DecimalToFraction[ -0.75 ] |
-3/4 |
The value stays exactly the same: 0.333333 is not 1/3. |
DecimalToFraction[ 0.333333 ] |
333333/1000000 |
| A script variable. | x := 3.14 |
r = 157/50 |
Details
- Integers give a fraction with denominator
1:DecimalToFraction[ 4 ]returns4/1andDecimalToFraction[ 2.0 ]returns2/1. - More than 10 decimals:
DecimalToFraction[ 0.12345678901 ]is left unevaluated. - Numbers without a finite decimal representation, such as
Pi, are left unevaluated.
| Invalid input | Result |
|---|---|
DecimalToFraction[ 1/3 ], DecimalToFraction[ x ] |
Error: the argument should be a decimal number |
DecimalToFraction[ {0.5, 0.25} ] |
Error: the argument should be a decimal number. Use Map for a list. |
DecimalToFraction[ 0.5, 1 ] |
Error: the number of arguments should be 1 |
See also: Numerator, Denominator, Numerical