LoginSignup
2
0

More than 1 year has passed since last update.

C# "1.23E-1"のような指数表示文字列を任意の数値型に変換する方法

Posted at

PCから外部の装置にコマンドで値を問い合わせたら指数表示でしかも文字列でかえってくることはよくあると思います。
その文字列からdouble型やint型の任意の型へと変換するメモです。

double型に変換.cs

string st = "1.23E-1";
double dec;

NumberStyles ns = NumberStyles.AllowExponent | NumberStyles.Float;
IFormatProvider fmt = CultureInfo.InvariantCulture;

//int.TryParseやdecimal.TryParseに変えることで任意の型へ変換可能
double.TryParse(st, ns, fmt, out dec);

MessageBox.Show(dec.ToString());
//→ 0.123

2
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0