[pstricks] Suggestions for \psPrintValue

Buddy Ledger buddyledger at gmail.com
Tue Apr 28 19:34:17 CEST 2009


>>/ This version changes two things. First, it now adds decplaces as a
/>>/ keyword to set the number of decimal places in the output. Second, if
/>>/ decplaces=-1, it returns all precision in the input. Eg,
/>>/ \fmtnum[decplaces=-1]{12.675E-002} outputs 0.12675.

Micheal,

Using your code as a guide I wrote the macro below.  It uses the fp package
for the heavy lifting.  I am having trouble with spaces in the output, I've tryed using 
\ignorespaces with no luck.  If you have any ideas about how to fix it, I'd appreciate it.

I wrote this to convert #.####### or ///#.#####E#// /-> #.##E# or #.##x10^# or #.##
/

Thanks,
Buddy

\documentclass[]{article}
\usepackage{pst-all}
\usepackage{pstricks-add}
\usepackage{amsfonts}
\usepackage{fp}


\makeatletter
%
\def\hasanE#1E#2\@nil{%check if expression contains an E--call with 
\hasanE#1E\@nil
\xdef\@tmpA{#1}%
\xdef\@tmpB{#2}%
\ifx\@tmpB\@empty %try lower case
  \expandafter\hasane\@tmpA e\@nil %
  \ifx\@tmpB\@empty %\@anEfalse %
  \else %
    %\@anEtrue %
    \expandafter\removee\@tmpB %
  \fi %
\else %
  %remove E from end of \@tmpB
  \expandafter\removeE\@tmpB %
%\@anEtrue
\fi}%
%
\def\hasane#1e#2\@nil{%check if expression contains an e--call with 
\hasane#1E\@nil
\xdef\@tmpA{#1}%
\xdef\@tmpB{#2}%
}%
%
\def\removeE#1E{\xdef\@tmpB{#1}}%
\def\removee#1e{\xdef\@tmpB{#1}}%
\def\hasperiod#1.#2\@nil{%check if expression contains .--call with 
\hasperiod#1.\@nil
% Check for value of \my at cntz to determine if exponent present
\xdef\@tmpA{#1}%
\xdef\@tmpB{#2}%
\ifx\@tmpB\@empty %\@periodfalse
\else %
%remove period from end of \@tmpB
\expandafter\removeper\@tmpB %
%\@periodtrue
\fi}%
%
\newif\if at period
\def\hasperiod#1.#2\@nil{
%check if expression contains .--call with \hasperiod#1.\@nil
% Check for value of \my at cntz to determine if exponent present
\def\@testA{#1}%
\def\@testB{#2}%
\ifx\@testA\@empty
  \ifx\@testB\@empty \else\@periodtrue\fi
\else %
\@periodfalse\fi}%
%
\newif\if at neg
\def\hasnegsign#1-#2\@nil{
%check if expression contains .--call with \hasnegsign#1-\@nil
% Check for value of \my at cntz to determine if exponent present
\xdef\@tmpA{#1}%
\xdef\@tmpB{#2}%
\ifx\@tmpB\@empty\@negfalse \else %
%remove neg from end of \@tmpB
\expandafter\removeneg\@tmpB %
\@negtrue\fi}%
\def\removeneg#1-{\xdef\@tmpA{#1}}%

\newif\if at ltone
\newif\if at isInt
\def\checkltone#10.#2\@nil{
%check if expression contains .--call with \hasperiod#1.\@nil
% Check for value of \my at cntz to determine if exponent present
\def\@testA{#1}%
\def\@testB{#2}%
\ifx\@testA\@empty
  \@ltonetrue
\else %
  \@ltonefalse
\fi}%

\def\checkint#1.#2\@nil{%
\def\testA{#1}
\def\testB{#2}
\ifnum\testB>\z@
  \@isIntfalse
\else
  \@isInttrue
\fi%
}

\def\addzero#1{%
\xdef\@tmpA{0#1}}%

\pst at addfams{pst-GetNum}
\define at key[psset]{pst-GetNum}{decplaces}{\def\psk at decplaces{#1}}
\define at boolkey[psset]{pst-GetNum}[Psk@]{science}[true]{}%
\define at boolkey[psset]{pst-GetNum}[Psk@]{printmath}[true]{}%
\psset[pst-GetNum]{decplaces=2,science=false}

\def\GetNum{\pst at object{GetNum}}
\def\GetNum at i{\@ifnextchar[{\GetNum at ii}{\GetNum at ii[]}}
\def\GetNum at ii[#1]#2{%#1=options,#2=number to format
{%
\use at par \ifx\#1\@empty \else\psset{#1}\fi%
\hasanE#2E\@nil
\xdef\bl at exponval{\@tmpB}
\expandafter\hasperiod\@tmpA.\@nil %set \@negtrue if neg, abs val in \@tmpA
\if at period
    \addzero{\@tmpA}
\fi%
\xdef\bl at decval{\@tmpA}
\ifx\bl at exponval\@empty %\relax
   \xdef\bl at exponval{0}
\fi
\FPpow{\tpB}{10}{\bl at exponval}
\FPmul{\tpA}{\tpB}{\bl at decval} %number value
%
\ifPsk at science
  \expandafter\hasnegsign\@tmpA-\@nil %set \@negtrue if neg, abs val in 
\@tmpA
  \if at neg
    \FPneg{\tpA}{\tpA}
  \fi%
  \expandafter\checkltone\tpA0.\@nil
  \FPln{\tpC}{\tpA}
  \FPln{\tpD}{10}
  \if at neg
    \FPneg{\tpA}{\tpA}
  \fi%
  \FPdiv{\tpE}{\tpC}{\tpD}
  \FPround{\inttest}{\tpE}{5}
  %\FPtrunc{\inttest}{\tpE}{5}
  \expandafter\checkint\inttest\@nil
  \if at ltone
    \if at isInt
    \FPtrunc{\bl at exponval}{\tpE}{0} %not exp rounded
    \else
    \FPadd{\bl at exponval}{\tpE}{-1}
    \FPround{\bl at exponval}{\bl at exponval}{3}
    \FPtrunc{\bl at exponval}{\bl at exponval}{0} %not exp rounded
    \fi
  \else
    \FPround{\bl at exponval}{\tpE}{3} %not exp rounded
    \FPtrunc{\bl at exponval}{\bl at exponval}{0} %not exp rounded
  \fi
  \FPpow{\tpB}{10}{\bl at exponval}
  \FPdiv{\bl at decval}{\tpA}{\tpB}
  \FPround{\bl at decval}{\bl at decval}{\psk at decplaces}
  \ifPsk at printmath
  \ensuremath{\bl at decval \times 10^{\number\bl at exponval}}
  \else
  \bl at decval E\number\bl at exponval
  \fi
\else
   \FPround{\bl at decval}{\tpA}{\psk at decplaces}
   \ifPsk at printmath
   \ensuremath{\bl at decval}
   \else
   \bl at decval
   \fi
\fi%
}\ignorespaces}
\makeatother

\begin{document}
\psset[pst-GetNum]{decplaces=2, science=false}

Test \verb|\GetNum| macro\newline
\vspace{1cm}\newline
The upper Limit of the FP packages fixed point capability is:\newline
\GetNum[science,printmath]{1.0E18}\newline
The lower Limit of the FP packages fixed point capability is:\newline
\GetNum[science,printmath]{1.0E-18}\newline
\vspace{1cm}

\begin{tabular}{|l|l|l|l|}\hline
Unformatted & Formatted Sci & Formatted Sci/Math & Formatted Dec \\\hline
12E-02 & \GetNum[science]{12E-02} & \GetNum[science,printmath]{12E-02} & 
\GetNum{12E-02}\\
-12e+004 & \GetNum[science]{-12e+004} & 
\GetNum[science,printmath]{-12e+004} & \GetNum{-12e+004}\\
-12e-004 & \GetNum[science]{-12e-004} & 
\GetNum[science,printmath]{-12e-004} & \GetNum{-12e-004}\\
-127.415 & \GetNum[science]{-127.415} & 
\GetNum[science,printmath]{-127.415} & \GetNum{-127.415}\\
99.995 & \GetNum[science]{99.995} &  \GetNum[science,printmath]{99.995} 
& \GetNum{99.995}\\
.01267 & \GetNum[science]{.01267} & \GetNum[science,printmath]{.01267} & 
\GetNum{.01267}\\
.1 & \GetNum[science]{.1} & \GetNum[science,printmath]{.1} & \GetNum{.1}\\
1.267E5 & \GetNum[science]{1.267E5} & 
\GetNum[science,printmath]{1.267E5} & \GetNum{1.267E5}\\
-12.6 & \GetNum[science]{-12.6} & \GetNum[science,printmath]{-12.6} & 
\GetNum{-12.6}\\
123456789.60 & \GetNum[science]{123456789.60} & 
\GetNum[science,printmath]{123456789.60} & \GetNum{123456789.60}\\\hline
\end{tabular}

\vspace{1cm}
\end{document}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: New.zip
Type: application/octet-stream
Size: 1616 bytes
Desc: not available
Url : http://tug.org/pipermail/pstricks/attachments/20090428/13d375cb/attachment.obj 


More information about the PSTricks mailing list