# How to define a function $f(x)$ such that $f(1.5)=1.5$, $f(1.)=1$, $f(0.)=0$, etc.?

How to define a function $f(x)$ such that $f(1.5)=1.5$, $f(1.)=1$, $f(0.)=0$, etc.? Namely, if $x$ is an integer with a decimal point, $f(x)$ returns the integer only, otherwise returns $x$.

This will be useful in the labels of the following plots.

Table[Block[{a = aa}, Show[Plot[x^a, {x, 0, 1}, PlotLabel -> Style[Row@{"a", "=", aa}, 15]]]], {aa, 0, 2, 0.5}]

-
I actually just figured out an answer: f[x_] := If[Abs[FractionalPart[x]] < \$MachineEpsilon, IntegerPart[x], x] – renphysics Apr 12 '14 at 13:02
f[x_] := If[FractionalPart[x] == 0, IntegerPart[x], x] – renphysics Apr 12 '14 at 13:12

There are numerous ways to accomplish what you're asking, but here's another way using a single definition:

f[x_]:=Piecewise[{{IntegerPart[x],x==IntegerPart[x]},{x,True}}]

-

I think the following definitions (you need both) will do want you're asking:

f[x_] := IntegerPart[x] /; x == IntegerPart[x]

f[x_] := x

-