Function Repository Resource:

NumberParse

Source Notebook

Parse a string to a number safely

Contributed by: Jesse Friedman

ResourceFunction["NumberParse"]["string"]

parses string to an integer or real number.

Details and Options

ResourceFunction["NumberParse"] returns $Failed if string could not be parsed safely.
ResourceFunction["NumberParse"] uses ToExpression to parse strings, but with protection to avoid evaluating unsafe inputs.
The following options are supported by ResourceFunction["NumberParse"]:
"AllowPrecision"Falsewhether to parse numbers specified with arbitrary precision
"AllowScientificNotation"Falsewhether to parse numbers specified in scientific notation
"AllowNonDecimal"Falsewhether to parse numbers specified in bases other than base 10
ResourceFunction["NumberParse"] has attribute Listable.

Examples

Basic Examples (4) 

Parse an integer:

In[1]:=
ResourceFunction["NumberParse"]["42"]
Out[1]=

Parse a real number:

In[2]:=
ResourceFunction["NumberParse"]["1.21"]
Out[2]=

Parse a negative number:

In[3]:=
ResourceFunction["NumberParse"]["-5"]
Out[3]=

$Failed will be returned if the string could not be parsed safely:

In[4]:=
ResourceFunction["NumberParse"]["not a number"]
Out[4]=

Scope (1) 

NumberParse threads automatically over lists:

In[5]:=
ResourceFunction["NumberParse"][{"42", "1.21", "-5", "not a number"}]
Out[5]=

Options (6) 

AllowPrecision (2) 

Arbitrary-precision numbers cannot be parsed with default settings:

In[6]:=
ResourceFunction["NumberParse"][{"3.14159`10", "1234.4321``3"}]
Out[6]=

Enable parsing of arbitrary-precision numbers:

In[7]:=
ResourceFunction["NumberParse"][{"3.14159`10", "1234.4321``3"}, "AllowPrecision" -> True]
Out[7]=

AllowScientificNotation (2) 

Numbers specified in scientific notation do not parse by default:

In[8]:=
ResourceFunction["NumberParse"]["1.234*^6"]
Out[8]=

Enable parsing of numbers specified in scientific notation:

In[9]:=
ResourceFunction["NumberParse"]["1.234*^6", "AllowScientificNotation" -> True]
Out[9]=

AllowNonDecimal (2) 

Numbers specified in bases other than 10 do not parse by default:

In[10]:=
ResourceFunction["NumberParse"]["16^^DEADBEEF"]
Out[10]=

Enable parsing of non-decimal numbers:

In[11]:=
ResourceFunction["NumberParse"]["16^^DEADBEEF", "AllowNonDecimal" -> True]
Out[11]=

Properties and Relations (3) 

ToExpression can also be used to parse numbers, but is dangerous to use with unfiltered input:

In[12]:=
ToExpression["1.21"]
Out[12]=
In[13]:=
ToExpression["Print[\"evaluating unfiltered, potentially malicious strings as code is unsafe\"]"]

NumberParse filters its input, evaluating only numeric content judged to be safe:

In[14]:=
ResourceFunction[
 "NumberParse"]["Print[\"evaluating unfiltered, potentially malicious strings as code is unsafe\"]"]
Out[14]=

FromDigits can also be used to safely parse numbers, but it only supports non-negative integers:

In[15]:=
FromDigits["42"]
Out[15]=
In[16]:=
FromDigits["1.21"]
Out[16]=
In[17]:=
FromDigits["-5"]
Out[17]=

NumberParse supports real and negative numbers:

In[18]:=
ResourceFunction["NumberParse"]["1.21"]
Out[18]=
In[19]:=
ResourceFunction["NumberParse"]["-5"]
Out[19]=

Interpreter["Number"] can also be used to safely parse numbers, but its performance can be unpredictable. By contrast, NumberParse has relatively stable, reasonably fast performance:

In[20]:=
ListLogPlot[{
  Table[AbsoluteTiming[Interpreter["Number"]["42"]], 5000][[All, 1]],
  Table[AbsoluteTiming[ResourceFunction["NumberParse"]["42"]], 5000][[
   All, 1]]
  }, PlotLegends -> {"Interpreter", "NumberParse"}, AxesLabel -> "seconds"]
Out[20]=

Possible Issues (1) 

NumberParse does not support complex or rational numbers:

In[21]:=
ResourceFunction["NumberParse"][{"1+2I", "22/7"}]
Out[21]=

Publisher

Jesse Friedman

Version History

  • 1.0.0 – 07 October 2019

Related Resources

License Information