Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Change language to: English - Français - 日本語 - Русский

See the recommended documentation of this function

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

• 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+"/"```

• evstr — avaliaÃ§Ã£o de expressÃµes
• isnum — testa se um string representa um número
• isdigit — checa se os caracteres de um string são números entre 0 e 9
• bin2dec — conversão de representação binária para inteira
• oct2dec — conversão de octais para inteiros
• hex2dec — conversão de representação hexadecimal para inteiros

### History

 Versão Descrição 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`.