Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - English


strtod

parse and convert literal numbers STRings TO Decimal numbers

Syntax

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

Arguments

str

single component, vector, or matrix of 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:

  • If it succeeds, at least for the heading part of str(i), the corresponding decimal number is returned in d(i). If any, the trailing part of str(i) is ignored and returned in tails(i); otherwise, tails(i) returns "".
  • Otherwise, d(i) returns %nan, and tails(i) is the whole str(i).

  • 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

  • evstr — evaluates Scilab expressions and concatenates their results
  • isnum — tests if a string represents a number
  • isdigit — check that characters of a string are digits between 0 and 9
  • bin2dec — convert from binary to decimal
  • oct2dec — convert from octal to decimal
  • hex2dec — convert from hexadecimal to decimal

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 >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jun 17 17:49:17 CEST 2024