gpt4 book ai didi

Delphi FloatToStr - 为什么显示不同?

转载 作者:行者123 更新时间:2023-12-03 15:17:53 26 4
gpt4 key购买 nike

使用DEFAULT FloatToStr函数

FloatToStr('0.0000442615029219009')

输出

4.42615029219009E-5

小数点后去掉一个零

FloatToStr('0.000442615029219009')

产生

0.000442615029219009

有人可以解释一下为什么第二种情况下的值没有输出到

4.42615029219009E-4

最佳答案

documentation for FloatToStr包含答案:

The conversion uses general number format with 15 significant digits.

要解释该声明,您还需要引用 topic describing the Format函数,特别是有关一般数字格式的文本(强调我的):

The value is converted to the shortest possible decimal string using fixed or scientific format. The number of significant digits in the resulting string is given by the precision specifier in the format string; a default precision of 15 is assumed if no precision specifier is present. Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses the fixed-point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format.

不幸的是,文档实际上是错误的。它应该读取 0.0001,而不是 0.00001。这个程序说明了这一点:

program FloatToStrScientificFixed;
{$APPTYPE CONSOLE}
uses
System.SysUtils;

var
d: Double;

begin
d := 0.0001;
Writeln(FloatToStr(d*0.9999999));
Writeln(FloatToStr(d));
Writeln(FloatToStr(d*1.0000001));
Readln;
end.

对于您的示例,0.0000442615029219009 小于 0.0001,因此使用科学计数法进行格式化。但 0.000442615029219009 大于 0.0001,因此使用固定表示法进行格式化。

如果您希望输出始终使用科学记数法,请使用 Formate format string .

<小时/>

QC#107388

关于Delphi FloatToStr - 为什么显示不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com