1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > float > floatの小数桁を調整してString型に変換 > str = FloatToStrF(314.15926535, ffFixed, 7,4); > 314.1593

Last updated at Posted at 2015-10-30
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

3.141592などのfloatを小数桁数を指定してString型に変換したい。

参考
http://www.delphibasics.co.uk/RTL.asp?Name=FloatToStrF

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	float pi100 = 314.15926535;

	String str;


	str = FloatToStrF(pi100, ffFixed, 7,4); // 1. 数値7桁, 小数4桁
	OutputDebugString(str.c_str());

	str = FloatToStrF(pi100, ffFixed, 5,4); // 2. 数値5桁, 小数4桁
	OutputDebugString(str.c_str());

	str = FloatToStrF(pi100, ffFixed, 7,3); // 3. 数値7桁, 小数3桁
	OutputDebugString(str.c_str());
}
結果
スレッドの開始 : スレッド ID: 3112. プロセス Project1.exe (2356)
スレッドの開始 : スレッド ID: 2996. プロセス Project1.exe (2356)
デバッグ出力: 314.1593 プロセス Project1.exe (2356)
デバッグ出力: 314.1600 プロセス Project1.exe (2356)
デバッグ出力: 314.159 プロセス Project1.exe (2356)

FloatToStr()の3番目の引数(7など)は数値をいくつまで保持するかということのようだ。
7の場合は、314(3つ) + 1593(4つ)まで保持するということ。

4番目の引数は小数の桁数 (1593)。
3桁とした場合、4桁目が四捨五入されるようだ (詳細は未確認)。

小数桁を4としていても、全体の桁数が5などになり314.16までしか保持されない場合、残りの小数桁は0になる。

1
1
1

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?