# Specify a decimal value as exact

I would like to specify that a number is exact, even though it contains a decimal point.

For example, I would like to be able to write some variation of 3.4 and have Mathematica interpret this as (exactly) 34/10, not the closest floating-point value to 3.4.

• Try Rationalize - reference.wolfram.com/language/ref/Rationalize.html Commented Dec 20, 2016 at 11:15
• Once you have rationalized 3.4 in 34/10, Mathematica will always automaticaly and systematically reduce it to 17/5. That's a problem for lisibility Commented Dec 20, 2016 at 12:48
• You can also use Round[3.4, 1/10] Commented Dec 20, 2016 at 13:43
• I don't care about the way the number is stored as a rational, just that it is an exact quantity. Commented Dec 20, 2016 at 16:15

To add to my comment, the basic way is Rationalize, but if you want to have the denominator always be a power of 10 I wrote a snippet that uses the length of the real number to determine the power of 10:

fraction[num_] := With[
(*Get the number of digits and the automatic rational*)
{digits = RealDigits[num], r = Rationalize[num]},
With[
{
(*Decide the power of 10 to be the denominator*)
d = 10^(Replace[First[digits], {x___, 0 ...} :> Length[{x}]] -Last[digits])
},
(*If an integer is entered leave it alone*)
If[
d == 1,
r,
With[
{n = Numerator[r]*d /Denominator[r]},
(*HoldForm stops Mathematica from evaluating this any more*)
HoldForm[n/d ]
]
]
]
]


It's a little complex but should be fairly robust.

• You can use the undocumented InternalRationalNoReduce[n, d] instead of HoldForm[n/d] so that it can still be treated as a number without having to keep invoking ReleaseHold[]. Commented Dec 20, 2016 at 13:58
• I don't care about the denominator being a power of 10. Commented Dec 20, 2016 at 16:19
• Given you explicitly stated exactly 34/10 in your question I thought it would be useful to have (also in response to andre's comment). Either way, the simple solution is at the start - Rationalize`. Commented Dec 20, 2016 at 16:34