strtonum - convert string to number


<strtonum $str [$flag]>

The strtonum function converts each of its string arguments to a numeric (double) value. The strings may be any text quantity that NPM (Metamorph's Numeric Pattern Matcher) can parse.

In version 7.07.1607103000 20201204 and later, an optional flag may be given:

  • bytepowersbinary: Interpret byte-related tokens like kilobyte, kb, megabyte, mb etc. as binary, i.e. powers of 1024. Tokens like kilohertz etc. that are not used in a byte context remain as powers of 1000.

  • bytepowersdecimal: Interpret byte-related tokens as decimal, i.e. powers of 1000. This is the default.

strtonum returns the double numeric value of each of its string arguments, or 0 if unparseable.


<$txt = "one thousand nine hundred ninety eight"
          "four score and seven years ago"
          "1/3"   "3.14"   "one hundred percent"
          "two thousandths"
          "nine and a half">
  <strtonum $txt>
  <LOOP $ret> $ret </LOOP>

The output would be:

1998 87 0.333333 3.14 1 0.002 9.5

The strtonum function was added in version 2.1.876800000 19971014.


Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.