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 want to convert my big integer number into a shorter form; for example

\convert{123456789} = 1.23 x 10^9

Is there any package which allows me to do so?

share|improve this question
    
How big is the big integer: 14 or fewer digits, or could it have a potentially arbitrary number of digits? After conversion to "scientific" format, how many digits should be retained? –  Mico Mar 8 at 13:44
    
Around 20+ digits is the size of the integers. I would like to make the number of retained digit configurable, but if not then 2 3 should be ok. Thanks –  Loi.Luu Mar 8 at 13:47

5 Answers 5

up vote 14 down vote accepted

The PGF math engine can do it.

In addition, the pgfplotstable package is quite powerful when it comes to rounding, formatting, and perhaps even postprocessing/generating numerical content.

\documentclass{standalone}

\usepackage{pgf}

\usepackage{pgfplotstable}

\begin{document}

\pgfmathprintnumber{123456789123456789123456789}

\pgfplotstabletypeset{
A B
1 123456789123456789123456789
2 9876543219876545432198765432222
}
\end{document}

enter image description here

share|improve this answer
    
Based on the comment on my answer, does this work with a pgfplotstable release from say 2009? –  Joseph Wright Mar 8 at 14:06
    
It works for my case. Thanks, –  Loi.Luu Mar 8 at 14:15
    
@JosephWright \pgfmathprintnumber works this way since its first release; pgfplotstable as well. If the precise date matters, I would need to take a closer look (I assume this is unnecessary for now). Sorry for the late response. –  Christian Feuersänger May 14 at 17:54

Not the core aim of the package, but siunitx does have lots of number-processing code built in:

\documentclass{article}
\usepackage{siunitx}
\begin{document}

$\num{123456789} =
  \num[round-precision = 3, round-mode = figures, scientific-notation = true]
    {123456789}$.

\end{document}

Note at present some of the internals of siunitx are limited to 'TeX-sized' numbers, but I am currently working on the next major release and this is one of the things to deal with. Fix for the present:

\documentclass{article}
\usepackage{siunitx,l3bigint}
\ExplSyntaxOn
\cs_set_protected:Npn
  \__siunitx_number_process_scientific_aux_ii:nnn #1#2#3 {
  \bigint_compare:nNnTF {#1} > { 9 }
    { \__siunitx_number_process_scientific_large:nnn {#1} {#2} {#3} }
    {
      \bigint_compare:nNnTF {#1} > { 0 }
        {
          \bool_if:NTF \l__siunitx_process_engineering_bool
            { \__siunitx_number_process_scientific_engineering:nnn }
            { \__siunitx_number_process_scientific_store:nnn }
               {#1} {#2} {#3}
        }
        {
          \__siunitx_number_process_scientific_small:wn
            #2 \q_stop {#3}
        }
    }
}
\cs_set_protected:Npn
  \__siunitx_number_process_scientific_large:nnn #1#2#3
  {
    \tl_set:Nx \l__siunitx_tmpa_tl
      { \bigint_div_truncate:nn {#1} { 10 } }
    \tl_set:Nx \l__siunitx_tmpb_tl
      { \bigint_mul:nn { \l__siunitx_tmpa_tl } { 10 } }
    \tl_set:Nx \l__siunitx_tmpb_tl
      { \bigint_sub:nn {#1} { \l__siunitx_tmpb_tl } }
    \tl_set:Nx \l__siunitx_tmpa_tl
      {
        { \l__siunitx_tmpa_tl } { \l__siunitx_tmpb_tl #2 }
        { #3 + 1 }
      }
  \exp_after:wN \__siunitx_number_process_scientific_aux_ii:nnn
    \l__siunitx_tmpa_tl
}
\ExplSyntaxOff
\begin{document}
$\num{1234567890123456789} =
  \num[round-precision = 3, round-mode = figures, scientific-notation = true]
    {1234567890123456789}$.

\end{document}
share|improve this answer
2  
@Loi.Luu I'm meant to be fixing one or two bugs to do with 'large' integers. However, I'm never really sure where these come up 'in the wild' in a place where rounding/truncation is legitimate. –  Joseph Wright Mar 8 at 13:46
    
Thanks, I got this ! Package xkeyval Error: round-precision undefined in families key'.` when inserted $\num[round-precision=3,round-mode=figures, scientific-notation = true]{933952}$ as a cell of a table. Am I doing wrong? –  Loi.Luu Mar 8 at 13:51
1  
@Loi.Luu Using version 1 of siunitx rather than version 2: is you have an older TeX set up then the choice of packages will be somewhat more limited –  Joseph Wright Mar 8 at 14:03
    
Is the l3bigint package part of the TeXLive (or MikTeX) distribution? –  Mico Mar 8 at 15:21
    
@Mico Part of l3trial, so to be fair you would have to grab that from the LaTeX3 master repo or GitHub. As I've tried to indicate, I need to think about some issues before updating the siunitx code in this area (and others). One thing I need to do is see if the team feel that 'big integers' are worth supporting. –  Joseph Wright Mar 8 at 15:25

The macro as you requested using again pgfmath

\documentclass[border=10pt]{standalone}

\usepackage{pgf}
\pgfkeys{/pgf/number format/.cd ,precision=2,sci generic={exponent={\times 10^{#1}}}}
\newcommand\convert[1]{\pgfmathprintnumber{#1}}

\begin{document}

\convert{123456789123456789123456789123456789123456789123456789}

\end{document}

enter image description here

share|improve this answer

Here's a solution that employs LuaLaTeX. There's a TeX-side macro called \tosci and a Lua-side function called tosci. The number of digits shown can be selected by modifying the first argument of the function string.format used by tosci.

The output of \tosci can be used by itself if you like the 1.234e+09 look. To get the 1.234 x 10^9 look, there's another TeX-side macro called \convert, which nests the \tosci macro inside the \num macro of the siunitx package. Note: I use the name \convert because that's the name you gave in your posting; you may want to come up with a more descriptive name.

Aside: A benefit of using \convert is that one needn't worry about the number being too big -- i.e., having too many digits -- for the current version of siunitx's \num macro to handle on its own.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{luacode} % for 'luacode' environment and '\luastring' macro
\usepackage{siunitx} % for '\num' macro

% Lua-side code
\begin{luacode} 
   function tosci ( n ) 
      return ( string.format ( "%.3e", n ) ) -- show 3 digits after the decimal
   end
\end{luacode}

% TeX-side code
\newcommand{\tosci}[1]{%
    \directlua{tex.sprint ( tosci ( \luastring{#1} ) ) }}
\newcommand{\convert}[1]{\num{\tosci{#1}}}

\begin{document}
\tosci{123456789012}, \tosci{1234123412341234}

\convert{456745674567456745674}
\end{document}
share|improve this answer

You can do this using in combination:

  1. the \xintFloat [P] macro of xintfrac, with P the arbitrary asked for precision, its output will be of the form <non zero digit>.<digit>...<digit>e<integer exponent>, with a total number of P digits in the significand,
  2. and the \numprint macro of package numprint which will transform such a thing into a customizable form, by default something like x.y \times 10^{exponent}, grouping digits by three, possibly with a separator.

The \xintFloat macro being expandable, one just need to nest the macro calls:

\numprint {\xintFloat [7]{123456789012345678901234567890}}
\numprint {\xintFloat [13]{123456789012345678901234567890}}

float print

The input to \xintFloat is not limited to be a big integer: it may be itself a scientific number such as 6.02e23, or a decimal number 2627.72827 without scientific exponent, or even a fraction whose numerator and denominator are each of the previous types or just big integers. For example 1234567890/9876543210: (the default precision is with 16 digits, the last one is rounded).

\numprint {\xintFloat {1234567890/9876543210}}

float print 2

source code:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{xintfrac}
\usepackage[autolanguage]{numprint}
\begin{document}

text mode (ttfamily):
\texttt{\numprint {\xintFloat [7]{123456789012345678901234567890}}} and
\texttt{\numprint {\xintFloat [13]{123456789012345678901234567890}}}

math mode:
$\numprint {\xintFloat [7]{123456789012345678901234567890}}$ and
$\numprint {\xintFloat [13]{123456789012345678901234567890}}$

\numprint {\xintFloat {1234567890/9876543210}}
\end{document}
share|improve this answer
    
Cool, thanks :) –  Loi.Luu Mar 9 at 5:01
    
@Loi.Luu I mentioned only numprint and its macro \numprint, but the \num of siunitx will work too (and \xintFloat will become unnecessary if siunitx adds support for big numbers to its rounding code). –  jfbu Mar 9 at 7:16

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.