strtod, _strtod_l, wcstod, _wcstod_l

將字串轉換成雙精確度值。

語法

double strtod(
   const char *strSource,
   char **endptr
);
double _strtod_l(
   const char *strSource,
   char **endptr,
   _locale_t locale
);
double wcstod(
   const wchar_t *strSource,
   wchar_t **endptr
);
double _wcstod_l(
   const wchar_t *strSource,
   wchar_t **endptr,
   _locale_t locale
);

參數

strSource
以 Null 終止的待轉換字串。

endptr
停止掃描的字元指標。

locale
要使用的地區設定。

傳回值

strtod 會傳回浮點數的值,但標記法會造成溢位時除外,在此情況下,函式會傳回 +/- HUGE_VAL 。 的 HUGE_VAL 正負號符合無法表示之值的正負號。 strtod0如果無法執行轉換或發生下溢,則傳回 。

wcstod 傳回類似 的值 strtod

  • 如果發生溢位或反向溢位,這兩個函式的 errno 都會設為 ERANGE
  • 如果參數無效, errno 則會設定為 EINVAL ,並叫用不正確參數處理常式,如參數驗證 中所述

如需此傳回碼和其他傳回碼的詳細資訊,請參閱 errno_sys_errlist_doserrno_sys_nerr

備註

每個函式都會將輸入字串 strSource 轉換成 doublestrtod 函式會將 strSource 轉換成雙精確度值。 strtod 停止在無法辨識為數字一部分的第一個字元讀取字串 strSource 。 此字元可能是終止的 Null 字元。 wcstod 是寬字元版本的 strtod,其 strSource 引數是寬字元字串。 除此之外,這些函式的行為相同。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

TCHAR.H 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcstod strtod strtod wcstod
_tcstod_l _strtod_l _strtod_l _wcstod_l

LC_NUMERIC目前地區設定的分類設定會決定 中 strSource 基數點字元的辨識。 如需詳細資訊,請參閱setlocale。 沒有後置詞的 _l 函式會使用目前的地區設定; _strtod_l_strtod 除了前者改用 locale 傳入 的 相同。 如需詳細資訊,請參閱 Locale

如果 endptr 不是 NULL ,則會將停止掃描的字元指標儲存在 所 endptr 指向的位置。 如果不能執行任何轉換 (找不到任何有效的數字或指定了無效的基底),則 strSource 的值會儲存在由 endptr 指向的位置。

strtodstrSource預期會指向下列其中一種形式的字串:

[whitespace] [sign] {digits [radixdigits] | radixdigits}[{e | E} [sign] digits]
[whitespace] [sign] {0x | 0X} {hexdigits [radixhexdigits] | radixhexdigits}[{p | P} [sign] digits]
[whitespace] [sign] {INF | INFINITY}
[whitespace] [sign] NAN [sequence]

選擇性前置 whitespace 詞可能包含空格和定位字元,這些字元會被忽略。
sign 是加號 (+) 或減號 (-)。
digits 是一或多個十進位數。
hexdigits 是一或多個十六進位數位。
radix 是預設 「C」 地區設定中的句點字元,如果目前的地區設定不同或指定時 locale ,則為地區設定特定的值。
sequence是英數位元或底線字元序列。

在十進位和十六進位數位表單中,如果沒有數位出現在基數點字元之前,至少必須出現一個在基數點字元之後。

在十進位格式中,小數位數後面可以接著指數,其中包含簡介字母 ( eE ) 和選擇性帶正負號的整數。

在十六進位形式中,十六進位數位後面可以接著指數,其中包含簡介字母 ( pP ) 和選擇性帶正負號的十進位整數,以 2 的乘冪表示指數。

在任一形式中,如果沒有指數部分或基數點字元,則會假設基數點字元會遵循字串中的最後一個數位。

NAN 表單中 INF 都會忽略大小寫。 不符合其中一種表單的第一個字元會停止掃描。

這些函式的 UCRT 版本不支援轉換 Fortran 樣式 ( dD ) 指數位母。 舊版 CRT 支援此非標準延伸模組,而且它可能是您程式碼的重大變更。 UCRT 版本支援十六進位字串和 和 NAN 值的來回, INF 這些和值在舊版中不支援。 此支援也會在您的程式碼中造成重大變更。 例如,字串 「 0x1a 」 會在舊版中解譯為 0.0,但在 UCRT 版本中會解譯 strtod 為 26.0。

需求

常式 必要的標頭
strtod, _strtod_l C:C++: <stdlib.h><cstdlib><stdlib.h>
wcstod, _wcstod_l C: <stdlib.h><wchar.h> C++: <cstdlib><stdlib.h><wchar.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_strtod.c
// This program uses strtod to convert a
// string to a double-precision value; strtol to
// convert a string to long integer values; and strtoul
// to convert a string to unsigned long-integer values.
//

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *string, *stopstring;
    double x;
    long   l;
    int    base;
    unsigned long ul;

    string = "3.1415926This stopped it";
    x = strtod(string, &stopstring);
    printf("string = %s\n", string);
    printf("   strtod = %f\n", x);
    printf("   Stopped scan at: %s\n\n", stopstring);

    string = "-10110134932This stopped it";
    l = strtol(string, &stopstring, 10);
    printf("string = %s\n", string);
    printf("   strtol = %ld\n", l);
    printf("   Stopped scan at: %s\n\n", stopstring);

    string = "10110134932";
    printf("string = %s\n", string);

    // Convert string using base 2, 4, and 8:
    for (base = 2; base <= 8; base *= 2)
    {
        // Convert the string:
        ul = strtoul(string, &stopstring, base);
        printf("   strtol = %ld (base %d)\n", ul, base);
        printf("   Stopped scan at: %s\n", stopstring);
    }

    // NaN
    x = strtod("+nan", &stopstring);
    printf("\n%f\n", x);

    // INF
    x = strtod("-INF", &stopstring);
    printf("\n%f\n", x);

    // e - exponent
    x = strtod("1.18973e+49", &stopstring);
    printf("\n%f\n", x);

    // doesn't handle Fortran style
    x = strtod("1.18973d+49", &stopstring);
    printf("\n%f\n", x);
    printf("No Fortran style support. Stopped parsing at %s\n", stopstring);
}
string = 3.1415926This stopped it
   strtod = 3.141593
   Stopped scan at: This stopped it

string = -10110134932This stopped it
   strtol = -2147483648
   Stopped scan at: This stopped it

string = 10110134932
   strtol = 45 (base 2)
   Stopped scan at: 34932
   strtol = 4423 (base 4)
   Stopped scan at: 4932
   strtol = 2134108 (base 8)
   Stopped scan at: 932

nan

-inf

11897299999999999421285862642874618947301378359296.000000

1.189730
No Fortran style support. Stopped parsing at d+49

另請參閱

資料轉換
數學和浮點支援
多位元組字元序列的解譯
地區設定
字串到數值函式
strtol, wcstol, _strtol_l, _wcstol_l
strtoul, _strtoul_l, wcstoul, _wcstoul_l
atof, _atof_l, _wtof, _wtof_l
localeconv
_create_locale, _wcreate_locale
_free_locale