TransWikia.com

pgfmath: rounded number without pgfmathprintnumber

TeX - LaTeX Asked on May 28, 2021

enter image description here

If I have a decimal number, say 0.09155, but I want to display a rounded version with two digits after the point: 0.09.
The standard way is to use pgfmathprintnumber[fixed, precision=2]{0.09155}.

So my question is: can I get the wanted value, here 0.09, by pgfmath-calculation, that means without using pgfmathprintnumber?

documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
pgfmathsetmacrox{0.09155}

$x = x$, but 
$x approx pgfmathprintnumber[fixed, precision=2]{x}$
end{document}

3 Answers

The following defines a roundn function for pgfmath that takes two arguments, one being the number to be rounded, the second being the number of places you want the number to round to.

documentclass[]{article}

usepackage{pgfmath}
pgfmathsetmacrox{0.09155}
pgfmathdeclarefunction{roundn}{2}
  {pgfmathparse{round(#1 * 10^(#2)) / (10^(#2))}}

begin{document}
$x = x$, but
$x approx pgfmathparse{roundn(x,2)}pgfmathresult$
end{document}

Correct answer by Skillmon on May 28, 2021

Here's a home-grown version with tokcycle (i.e., no pgf). It rounds to a level specified in savedigits.

documentclass{article}
usepackage{tokcycle}
newcounter{decs}
defsavedigits{2}
newififerr
newififfounddot
newcommandroundit[1]{%
  errfalse
  setcounter{decs}{0}%
  founddotfalse% MADE T WHEN DECIMAL HAS BEEN 1ST LOCATED
  tokcycle% CYCLE THROOUGH EACH TOKEN
  {tctestifx{.##1}%
   {addcytoks{##1}%
    iffounddoterrtruefifounddottruesetcounter{decs}{0}}% IF .
   {tctestifnum{`##1>`/}%
    {tctestifnum{`##1<`:}%
     {%
       iffounddotstepcounter{decs}fi%
       ifnumthedecs<savedigitsrelaxaddcytoks{##1}else
       ifnumthedecs=savedigitsrelaxaddcytoks{roundlast ##1}else
       ifnumthedecs=numexprsavedigits+1 addcytoks{##1}fififi
     }%
     {errtrue}% IF ASCII > `9
    }%
    {errtrue}% IF ASCII < `0
   }%
  }% APPLY ABOVE LOGIC FOR CHAR TOKENS
  {errtrue}% IF BRACES
  {errtrue}% IF CONTROL SEQUENCE
  {}% IGNORE SPACES
  {#1}% THE ARGUMENT
  addcytoks{space}
  iferr ERRORelsethecytoksfi
}
newcommandroundlast[2]{ifnum0#2>4 thenumexpr#1+1 else#1fi}
begin{document}
noindent
0) roundit{0.09155}
1) roundit{12.3}
2) roundit{0.127}
3) roundit{1.2.3}
4) roundit{1.3today}
5) roundit{321.345 678}
6) roundit{000321.305}
7) roundit{.006 300 345}
8) roundit{0003x1.345}
9) roundit{1230}
9a) roundit{1230.}
A) roundit{123.078}
B) roundit{0003;345}
end{document}

enter image description here

Answered by Steven B. Segletes on May 28, 2021

Why not with xfp package?

screenshot

documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
usepackage{xfp}
begin{document}
pgfmathsetmacrox{0.09155}

$x = x$, but 
$x approx pgfmathprintnumber[fixed, precision=2]{x}$

bigskip
With xfp package:

$x = x$, but 
$x approx fpeval{round(x,2)}$

end{document}

Answered by AndréC on May 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP