Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to automatically format numbers in a way that doesn't convert say .8 to 0.8 while at the same time scientific notation is converted to exponent-free notation. siunitx seems to be able to do both but I don't get it to work together.

\documentclass{article}
\usepackage{siunitx}
\newcommand{\myround}[1]{\num[zero-decimal-to-integer,scientific-notation=fixed,fixed-exponent=0,round-mode=places,round-precision=2,add-integer-zero=false]{#1}}
%\newcommand{\myround}[1]{\num[zero-decimal-to-integer,round-mode=places,round-precision=2,add-integer-zero=false]{#1}}
\pagestyle{empty}
\begin{document}
\myround{.8}
\myround{1}
\myround{4.00000000001}
\myround{-.1151}
\myround{4.44089209850063E-16}

.80
1
4
-.12
0
\end{document}

The first \newcommand in this MWE IMHO should produce the right result because it got all the right options. However, the zeros before the decimal point are added back in:

zeros still there

Using the second macro definition (that is commented out) yields this result, proving that in principle it should work but of course the scientific notation isn't converted and not rounded properly either (rounding to two decimal places, 4.44×10-16 should be 0.00×100 if anything).

scientific notation still there

I also added the pgfmath tag because \pgfmathprintnumber seems to have much of the same options as \num from siunitx and I don't know if one uses the other internally and I also wouldn't mind to use that other if it does what I need.

share|improve this question
    
Have a look at the numprint package! –  Keks Dose Mar 12 '13 at 17:14
1  
The scientific notation doesn’t allow .80 (or 0.80 for that matter). PGF’s math printing number and siunitx are independent (though you can send siunitx’ settings to PGF). –  Qrrbrbirlbel Mar 12 '13 at 17:19
    
@Jake Sorry I didn't make that clear: the second line is the desired output. –  Christian Mar 12 '13 at 17:20
    
@Qrrbrbirlbel Good to know. The thing is, I don't want scientific notation but I occasionally get it on the input side and to get rid of it I need those options. –  Christian Mar 12 '13 at 17:24
1  
The current siunitx code works such that the two operations here are entirely separate. Forcing a fixed exponent is set up such that the output must have an integer part even if it's zero. I can of course look to alter this behaviour: the problem is getting the 'expected' interactions correct. –  Joseph Wright Mar 13 '13 at 9:01
show 10 more comments

1 Answer

up vote 2 down vote accepted

\pgfmathprintnumber can do this if you specify skip 0. and fixed:

\documentclass{article}
\usepackage{tikz}
\newcommand{\myround}[1]{\pgfmathprintnumber[skip 0.,fixed]{#1}}
\pagestyle{empty}
\begin{document}
\myround{.8}
\myround{1}
\myround{4.00000000001}
\myround{-.1151}
\myround{4.44089209850063E-16}

.80
1
4
-.12
0
\end{document}
share|improve this answer
    
This is almost perfect, thanks! If you know a way to set the numbers in the surrounding font instead of always in math font, that would be perfect. If not I have to change the parts where I still have to use siunitx because I use actual units and go through my document if I somewhere still use numbers without any surrounding macro. –  Christian Mar 12 '13 at 19:09
    
@Christian: No, sorry, I don't know how to do that off the top of my head. It's probably best if you open a new question for this along with an example document. –  Jake Mar 12 '13 at 22:38
    
I guess just fixing my document is easier. I forgot to mention that I did find assume math mode=true which switches off ensuremath but that doesn't seem like the right way accomplish that. Anyway, as I said, this will do just fine. Thanks! –  Christian Mar 12 '13 at 22:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.