# PGF: Varied Number of Decimal Places

I am making a macro to help show kids how to estimate, say, 34.1% of 5.

I just can't get the calculations to output exact numbers with appropriate (minimal) decimal places. Instead, PGF is showing me that 40% of 5 is 1.99997

How do I fix that? And what about in the future when I may want to specify a maximum number of decimal places?

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf}
\usepackage[margin=1.5cm]{geometry}

\title{Percents of Whole Numbers on a Number Line}
\date{}
\author{}

\setlength{\parindent}{0pt}

\newcommand{\percentA}[2]
{
\begin{tikzpicture}[xscale=18]
\foreach \x in {0,1,...,#1}
{
\node at (\x/#1,0.45) {\x}; % Whole numbers
\draw [very thick] (\x/#1,0)--(\x/#1,0.25);  % Whole number tick marks
}

\draw[very thick] (0,0)--(1,0); % x axis

\foreach \x in {0,...,100} % Hundredths tick marks
\draw (\x/100,-0.1)--(\x/100,0);

\foreach \x in {0,1,2,...,10}
{
\draw[thick] (\x/10,-0.18)--(\x/10,0); % Tenths tick marks
\pgfmathsetmacro{\tenspercents}{int(\x*10)}
\node at (\x/10,-0.6) {\tenspercents \%};
\node at (\x/10,-1) {of #1};
\pgfmathsetmacro{\result}{\tenspercents/100*#1,}
\node at (\x/10,-1.4) {is \result};
}
\end{tikzpicture}
}

\begin{document}

\pagestyle{empty}

\maketitle

\percentA{5}

\end{document}


That code yields this:

• The answer is really just the same as the one to your previous question. But why do you have a comma in \result?
– cfr
Sep 13, 2016 at 1:07
• As written, your code will no longer compile. The version you posted earlier compiled fine, but now you've told LaTeX to expect 2 arguments but only provided 1.
– cfr
Sep 13, 2016 at 2:01
• I think I fixed it. That code compiles on my computer. Sep 13, 2016 at 4:10
• I fixed it once. You've unfixed it. You are telling LaTeX to expect 2 arguments but only giving it 1.
– cfr
Sep 13, 2016 at 10:05

As I said in response to your other question, you want to format the number to pretty-print it. In this case, I'm guessing the default format will be suitable, but you can obviously tune it according to your particular needs.

\documentclass[border=10pt,multi,tikz]{standalone}
\newcommand{\percentA}[2]
{%
\begin{tikzpicture}[xscale=18]
\foreach \x in {0,1,...,#2}
{
\node at (\x/#2,0.45) {\x}; % Whole numbers
\draw [very thick] (\x/#2,0)--(\x/#2,0.25);  % Whole number tick marks
}

\draw[very thick] (0,0)--(1,0); % x axis

\foreach \x in {0,...,100} % Hundredths tick marks
\draw (\x/100,-0.1)--(\x/100,0);

\foreach \x in {0,1,2,...,10}
{
\draw[thick] (\x/10,-0.18)--(\x/10,0); % Tenths tick marks
\pgfmathsetmacro{\tenspercents}{int(\x*10)}
\node at (\x/10,-0.6) {\tenspercents \%};
\node at (\x/10,-1) {of #2};
\pgfmathsetmacro{\myresult}{\tenspercents/100*#2}
\node at (\x/10,-1.4) {is \pgfmathprintnumber{\myresult}};
}
\end{tikzpicture}%
}
\begin{document}
\percentA{20}{5}
\end{document}


The default maths stuff is never going to be accurate. If you need accuracy, you can use floating point, for example. But I doubt that is really needed for grade 2 maths (assuming this is still for your second graders). Just printing the numbers to make them prettier is all that's really required.

# EDIT

This part of my answer uses a corrected version of the alternative version of \percentA which you posted later. Unlike the first version, this only uses 1 argument and your example only gave 1 argument. Unfortunately, like the first version, it still demanded 2.

Also note that both versions introduce spurious spaces. I corrected this above and have annotated the corrections for the alternative version below.

Also also note that you should not include a comma in the argument you expect pgfmath to parse as a number. This will create problems. For example, the number cannot be formatted correctly afterwards because it contains alien stuff. Again, I corrected that above and repeat the correction with an annotation below.

The code below is designed to demonstrate a few of the various most-relevant-to-second-grader ways in which a number can be formatted by pgfmath. For other options, such as scientific formats, see TikkZ's manual where the options are extensively described.

The following gives the output of

\percentA{5}


for a single definition of \percentA{}. That is, the numbers being printed are the same in every case. All that changes are relevant settings for /pgf/number format, as given in the code which follows.

Remember: the series of values held by \result are identical for each of the number lines, because the definition of \percentA{} is held constant. What changes is the effect of the macro \pgfmathprintnumber{} when fed the series of values in \result as arguments.

\documentclass[border=10pt,multi,tikz]{standalone}
\newcommand{\percentA}[1]% if you are only going to give one argument, don't say you'll give two
{% avoid introducing spurious spaces
\begin{tikzpicture}[xscale=18]
\foreach \x in {0,1,...,#1}
{
\node at (\x/#1,0.45) {\x}; % Whole numbers
\draw [very thick] (\x/#1,0)--(\x/#1,0.25);  % Whole number tick marks
}

\draw[very thick] (0,0)--(1,0); % x axis

\foreach \x in {0,...,100} % Hundredths tick marks
\draw (\x/100,-0.1)--(\x/100,0);

\foreach \x in {0,1,2,...,10}
{
\draw[thick] (\x/10,-0.18)--(\x/10,0); % Tenths tick marks
\pgfmathsetmacro{\tenspercents}{int(\x*10)}
\node at (\x/10,-0.6) {\tenspercents \%};
\node at (\x/10,-1) {of #1};
\pgfmathsetmacro{\result}{\tenspercents/100*#1}% comma is not part of number!!
\node at (\x/10,-1.4) {is \pgfmathprintnumber{\result}};
}
\end{tikzpicture}% avoid introducing spurious spaces
}
\begin{document}
\percentA{5}
\tikzset{%
/pgf/number format/.cd,
fixed,
precision=2,
}
\percentA{5}
\tikzset{%
/pgf/number format/.cd,
fixed zerofill,
}
\percentA{5}
\tikzset{%
/pgf/number format/.cd,
precision=0,
}
\percentA{5}
\tikzset{%
/pgf/number format/.cd,
int trunc,
}
\percentA{5}
\end{document}


An alternative way which requires lualatex for the percentage calculations. There are also some other optimisations (which don't require lualatex):

\RequirePackage{luatex85}% Currently needed in TeXLive 2016
\documentclass[border=5,varwidth=20cm]{standalone}
\usepackage{tikz}
\newcommand{\percentA}[2][1]
{%
\begin{tikzpicture}[xscale=18]
\foreach \x in {0,#1,...,#2}
\draw [very thick] (\x/#2,0)--(\x/#2,0.25)
node [above] {\x};  % Whole number tick marks

\draw[very thick] (0,0)--(1,0); % x axis

\foreach \x in {0,...,100} % Hundredths tick marks
\draw (\x/100,-0.1)--(\x/100,0);

\foreach \x in {0,1,2,...,10}
{
\edef\tenspercents{\directlua{tex.print(\x*10)}}
\edef\myresult{\directlua{tex.print(\tenspercents/100*#2)}}
\draw[thick] (\x/10,0)--(\x/10,-0.18)
node [at end, below, align=center]
{\tenspercents \% \\ of #2 \\ is \myresult};
}
\end{tikzpicture}%
}
\begin{document}
\foreach \p/\s in {5/1, 10/1, 15/1, 20/2, 50/5, 400/50}{
\percentA[\s]{\p}
\\[1em]
}
\end{document}