I want to do the following: I am generating a report which can be set to be in imperial or metric units. All values are originally in metric units, so in the case of imperial units all values should be converted. Some values can be say N/A or any other string value, in case the value is not applicable. The document is automatically generated. For conversion I am using the fp package, but the challenge is the checking the input. The fp package (and pgfmath) will ofcourse) err if you try to do math with non-numerical values. This is what I currently have (only works for integer input)

% Convert meters to inches
    \FPeval{val}{#1*39.3700787} \FPround{\val}{\val}{#2}

Using pgfmath one could do something like this

    \pgfmathfloatifflags{\pgfmathresult}{3}{\FPeval{val}{#1*39.3700787} \FPround{\val}{\val}{#2}}{\def\val{#1}} 

But I am not able to set the error handler of pgfmath to output NaN in the case of erronous input to pgfmathfloatparsenumber

\pgfkeys{/pgf/fpu/handlers/invalid number={??}{??}}

Thanks for any help!


4 Answers 4


You can also use \IfDecimal from the xstring package:

enter image description here



        ``#1" is a number.%
        ``#1" is NOT a number.%

\par\CheckIfNumerical{7. 0}

datatool provides conditionals for testing whether an argument is numerical or not:

enter image description here

\usepackage{datatool}% http://ctan.org/pkg/datatool







There is \DTLisnumerical and \DTLifnumerical. See section 2.2 ifthen conditionals (p 16) of the datatool user guide.


UPDATED to handle negatives.

A call to \testreal sets up a recursive loop that checks every byte in the string.

First, it strips off a leading negative, if it exists, since that will not affect if the remainder of the string is or is not a valid real number.

With what remains, here is how it proceeds. It notes that no digit nor decimal point has yet been found in the string, but assumes that it is a valid real number.

Going through each successive byte, the test will toggle to the "fail" state if it locates a non-decimal, non-digit. The test will also fail if it finds two decimal points in the string. But even if both those tests are passed, it must have found at least one digit, to pass the test.


  \if T\founddigit\itsanumber\else F\fi}

\if.#1\if T\fndpt\def\itsanumber{F}\else\def\fndpt{T}\fi\else


\testreal{just}    \testreal{-just}

\testreal{123John} \testreal{-123John}

\testreal{324.56}  \testreal{-324.56}

\testreal{.}       \testreal{-.}

\testreal{23.4.56} \testreal{-23.4.56}

\testreal{346}     \testreal{-346}

enter image description here


I didn't quite get what your intention is but here is a quick example for coming up with PGF solutions.


\pgfmathfloatifflags{\pgfmathresult}{3}{%True Not a number
    }{% False it is a number including inf

\pgfkeys{/pgf/fpu/handlers/invalid number/.code={%

\pgfmathfloatparsenumber{435...}% NaN
\pgfmathfloatparsenumber{la la laa}% NaN



This will give





  • 1
    Is it possible to put out the content, instead of NaN?
    – cis
    Oct 21, 2018 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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