😸

[C] strtod 関数

に公開

機能

文字列を倍精度浮動小数点数(double型)に変換する関数[1]

使用例

123.456 と表示する

\n は改行を表す エスケープシーケンス[2]

コード

printf("%f\n", strtod("123.456", NULL));
全文
#include <stdio.h>
#include <stdlib.h>
void main() {
    printf("%f\n", strtod("123.456", NULL));
}

実行結果

123.456000

0.789 を変数から表示する

コード

const char *numstr = "0.789";
double num = strtod(numstr, NULL);
printf("%f\n", num);
全文
#include <stdio.h>
#include <stdlib.h>
void main() {
    const char *numstr = "0.789";
    double num = strtod(numstr, NULL);
    printf("%f\n", num);
}

実行結果

0.789000
脚注
  1. strtodは文字列中の数値部分をdouble型に変換し、変換できなかった部分のポインタも取得できる。 ↩︎

  2. 改行やタブなど、画面に表示されない制御文字のこと。 ↩︎

Discussion