MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Is there ToNumber?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119623] Re: Is there ToNumber?
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 14 Jun 2011 07:51:33 -0400 (EDT)
  • References: <isnllk$n7b$1@smc.vnet.net> <201106090945.FAA06238@smc.vnet.net> <isv78n$80$1@smc.vnet.net>

Hi,

the short answer is: no.

> Is there a built-in function which returns the numeric value of a string
> (like ToString but inverse).  Currently I use mine function toNumber:
>
>    ch = StringToStream[timeStr];
>    timeNum = Read[ch, Number];
>    Close[ch];
>
> but I suspect there is easier way.

The question is what kind of input you expect "ToNumber" to handle. As 
others have pointed out ToExpression is the most simple and also 
relatively fast but only works for numbers given in Mathematica syntax. 
If you need to handle other formats, then there are alternatives to what 
you do, but they seem to be slower if used in a naive way, e.g.:

ImportString["1e3", "Table"][[1, 1]]

whether you consider that to be "easier" is another question. What about 
defining:

toNumber[s_String] := Module[{ch, res},
   ch = StringToStream[s];
   res = Read[ch, Number];
   Close[ch];
   res
   ];

This is easy to use and also relatively fast. Is there a reason why you 
think that is not good enough? If you need further advice, I think it 
would be necessary to specify where these strings come from...

hth,

albert


  • Prev by Date: Re: Question with ToExpression
  • Next by Date: Mathworld packages
  • Previous by thread: Re: Is there ToNumber?
  • Next by thread: Re: Is there ToNumber?