<< strsubst Strings strtok >>

Scilab Help >> Strings > strtod

strtod

parse and convert literal numbers STRings TO Decimal numbers

Syntax

d =  strtod(str [,decimalseparator])
[d, tails] = strtod(str [,decimalseparator])

Arguments

str

single component, vector, or matrix of text (strings).

decimalseparator

the decimal separator chosen: "." (by default) or ",".

d

scalar, vector, or matrix of decimal numbers.

tails

a string or matrix of strings: remaining parts of str after the numerical heads (if any).

Description

[d, tails] = strtod(str) parses each string of str and tries to interpret its content as a decimal number:

  • Complex numbers: strtod(..) parses and returns only their real part, provided that it is writen before their imaginary part.
  • strtod(..) does not acknowledge any thousands separator, neither " ", nor "," nor any other one.
  • strtod(..) parses strings only against decimal numbers. It does not interpret hexadecimal, octal or other radix strings.
  • strtod(..) does not interpret any name of Scilab predefined or user-defined variables or special strings for %inf or %nan. Hence we have:
    s = ["-0.034" "- 0.034"  "+1234.5678" "1234 5678"
         "-.764"  "+.432"    "12,231.7"   "-5.458,871"
         "1e43"   "-3.5d-12" "-1.2+i"     "i+1.2"
         ""       "%inf"     "Inf"        "-Inf"
         "%i"     "%pi"      "%e"         "%eps"
         "%F"     "0x19B"    "#14C4"      "o5745"
         ]
    strtod(s)
      s  =
    !-0.034  - 0.034   +1234.5678  1234 5678   !
    !-.764   +.432     12,231.7    -5.458,871  !
    !1e43    -3.5d-12  -1.2+i      i+1.2       !
    !        %inf      Inf         -Inf        !
    !%i      %pi       %e          %eps        !
    !%F      0x19B     #14C4       o5745       !
    
    --> strtod(s)
     ans  =
      -0.034       Nan         1234.5678   1234.
      -0.764       0.432       12.        -5.458
       1.000D+43  -3.500D-12  -1.2         Nan
       Nan         Nan         Nan         Nan
       Nan         Nan         Nan         Nan
       Nan         0.          Nan         Nan
    
  • strtod(..) does not interpret escape sequences "\n", "\t", etc.
  • This function is based on the strtod C function which causes different behaviors on Windows and Linux. In fact, on Windows, it is possible to use "d" or "D" for exponents, but it is not possible to use hexadecimal numbers.

Examples

s = ["123.556 abc " ".543"      "#58B" "0x73 " "%inf"
     "-1.47e-71"    "67,432.57" " 23,5" "-,57" "Inf" ]
[num, trail] = strtod(s);
num, "/"+trail+"/"

// With "," as decimal separator:
[num, trail] = strtod(s, ",");
num, "/"+trail+"/"

See also

History

VersionDescription
5.5.0 Option decimalseparator introduced (SEP 97).
5.4.1 If str does not contain any numerical value, d now returns Nan instead of 0.

Report an issue
<< strsubst Strings strtok >>