當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ strtod()用法及代碼示例


strtod()是C和C++ STL中的內置函數,該函數將字符串的內容解釋為浮點數,並將其值返回為雙精度值。它將指針設置為指向字符串的最後一個有效字符之後的第一個字符(僅在存在字符串時),否則將指針設置為null。

用法

double strtod(str, &end)

參數
str:它指定具有浮點數表示形式的字符串。
end:指定一個參數,該參數表示char *類型的已分配對象。


返回值:它返回一個雙精度值(從字符串中轉換),如果無法執行有效的轉換,則返回0。

以下示例程序旨在說明上述函數:

程序1:

// C++ program to illustrate the 
// strtod() function 
#include <cstdlib> 
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
    char str[] = "11.03e 0mn"; 
    char* end; 
    double number; 
  
    number = strtod(str, &end); 
    cout << "number = " << str << endl; 
    cout << "double = " << number << endl; 
    cout << "end string = " << end << endl; 
  
    return 0; 
}
輸出:
number = 11.03e 0mn
double = 11.03
end string = e 0mn

程序2:

// C++ program to illustrate the 
// strtod() function without trailing characters 
#include <cstdlib> 
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
    char str[] = "4.06"; 
    char* end; 
    double number; 
  
    number = strtod(str, &end); 
    cout << "number= " << str << endl; 
    cout << "double= " << number << endl; 
  
    // If end is not Null 
    if (*end) { 
        cout << end; 
    } 
    // If end is Null 
    else { 
        cout << "null"; 
    } 
    return 0; 
}
輸出:
number= 4.06
double= 4.06
null

程序3:
strtod()函數,具有 index 和十六進製

// C++ program to illustrate the 
// strtod() function with exponents and hexadecimals 
#include <cstdlib> 
#include <cstring> 
#include <iostream> 
  
using namespace std; 
int main() 
{ 
    // initialize a exponential value 
    char str[] = "-89.04e-3win gfg"; 
    char* end; 
    double number; 
  
    number = strtod(str, &end); 
    cout << "str = " << str << endl; 
    cout << "double = " << number << endl; 
    cout << "end string = " << end << endl 
         << endl; 
  
    // initialize a new hexadecimal value 
    strcpy(str, "1998gupta.1204ishwar"); 
  
    number = strtod(str, &end); 
    cout << "str = " << str << endl; 
    cout << "double = " << number << endl; 
    cout << "end string = " << end << endl; 
  
    return 0; 
}
輸出:
str = -89.04e-3win gfg
double = -0.08904
end string = win gfg

str = 1998gupta.1204ishwar
double = 1998
end string = gupta.1204ishwar

程序4:

// C++ program to illustrate the 
// strtod() function for Infinity and NaN 
#include <cstdlib> 
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
    char* end; 
  
    cout << "Infinity"
         << " to double = " 
         << strtod("infinity", &end) << endl; 
    cout << "end string = " << end << endl 
         << endl; 
  
    cout << "Infpqrs"
         << " to double = " <<  
         strtod("Infpqrs", &end) << endl; 
    cout << "end string = " << end << endl 
         << endl; 
  
    cout << "NaN11x"
         << " to double = " 
         << strtod("NaN11x", &end) << endl; 
    cout << "end string = " << end << endl 
         << endl; 
  
    return 0; 
}
輸出:
Infinity to double = inf
end string = 

Infpqrs to double = inf
end string = pqrs

NaN11x to double = nan
end string = 11x

程序5:
strtod()函數具有領先的空白

// C++ program to illustrate the 
// strtod() function with leading whitespace 
#include <cstdlib> 
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
    char* end; 
  
    cout << "99.99"
         << " to double = " 
         << strtod(" 19.99", &end) << endl; 
    // end pointer is set to null 
    cout << "end string = " 
         << end << endl 
         << endl; 
  
    // Returns 0 because of invalid conversion 
    cout << "xyz1.80"
         << " to double = " 
         << strtod("xyz1.80", &end) << endl; 
    cout << "end string = " << end << endl 
         << endl; 
  
    return 0; 
}
輸出:
99.99 to double = 19.99
end string = 

xyz1.80 to double = 0
end string = xyz1.80


相關用法


注:本文由純淨天空篩選整理自IshwarGupta大神的英文原創作品 strtod() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。