GeneralNumerical values format

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
one_eddie
Posts: 13
Joined: Thu Sep 04, 2008 11:13 am

Numerical values format

Post by one_eddie »

Does LaTeX enable to format float values or add some sort of custom handler?
I don't event know what should it be :)

I know I would like to use something like this \format{0.00016493240} and get the output like 1.649324\cdot10^{-4}.

Is there something I could use?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Re: Numerical values format

Post by localghost »

LaTeX is a typesetting system and not a math engine.


Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
one_eddie
Posts: 13
Joined: Thu Sep 04, 2008 11:13 am

Re: Numerical values format

Post by one_eddie »

You are right, but this is just a matter of seeing the value. So I've thought LaTeX could be able to do that.
Thanks for the lesson. I will write a small program to generate TeX I need.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Numerical values format

Post by T3. »

one_eddie wrote:I know I would like to use something like this \format{0.00016493240} and get the output like 1.649324\cdot10^{-4}.

Is there something I could use?
There are several packages providing such functionality. Take a look at numprint, siunitx and pgf/TikZ and see if any of them suits your needs. PGF even comes with math engine, so you can use it for some simple calculations.

Cheers,

Tomek
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Numerical values format

Post by Juanjo »

I haven't been able to find, in the documentation of numprint, siunitx and pgf/TikZ, a command doing exactly what the OP is asking for, so I've defined it. For positive numbers, the function \format in the example below seems to work as expected:

Code: Select all

\documentclass{article}
\usepackage{fp,siunitx}

\newcommand{\format}[2][5]{% format #2 rounding the mantissa to #1 digits (5 by default) 
 \FPln\ExpX{#2}%
 \FPln\lnTen{10}%
 \FPdiv\ExpX\ExpX\lnTen%
 \FPifpos\ExpX% #2 is a number >=1
   \FPtrunc\ExpX\ExpX{0}%
   \FPpow\MantX{10}{\ExpX}%
   \FPdiv\MantX{#2}\MantX%
   \FPround\MantX\MantX{#1}%
 \else% #2 is a number between 0 and 1
   \FPadd\ExpX\ExpX{-1}%
   \FPtrunc\ExpX\ExpX{0}%
   \FPneg\nExpX\ExpX%
   \FPpow\MantX{10}{\nExpX}%
   \FPmul\MantX{#2}\MantX%
   \FPround\MantX\MantX{#1}%
 \fi%
 \num[expproduct=cdot]{\MantX e\ExpX}}

\newcommand{\printexample}[1]{\num{#1} = \format{#1}\par}

\begin{document}

\printexample{1234567.89}
\printexample{123456.789}
\printexample{12345.6789}
\printexample{1234.56789}
\printexample{123.456789}
\printexample{12.3456789}
\printexample{1.23456789}
\printexample{0.123456789}
\printexample{0.0123456789}
\printexample{0.00123456789}
\printexample{0.000123456789}
\printexample{0.0000123456789}

\end{document}
The \num command is defined in siunitx and the \FP ones, in fp. It seems to me that computations done by the fp package are more precise that those done by the math engine provided by pgf/TikZ. There is surely a better way to define \format.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
one_eddie
Posts: 13
Joined: Thu Sep 04, 2008 11:13 am

Re: Numerical values format

Post by one_eddie »

Thank you very much.
This is what I was asking about.
Post Reply