Using the DEFAULT FloatToStr function
FloatToStr('0.0000442615029219009')
outputs
4.42615029219009E-5
dropping one zero after the decimal place
FloatToStr('0.000442615029219009')
produces
0.000442615029219009
Can someone please explain why the value in the second case is not output to
4.42615029219009E-4
The documentation for FloatToStr
contains the answer:
The conversion uses general number format with 15 significant digits.
To interpret that statement you need also to refer to the topic describing the Format
function, and specifically the text concerning the general number format (emphasis mine):
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.
Unfortunately the documentation is in fact in error there. Instead of 0.00001
it should read 0.0001
. That this is illustrated by this program:
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.
For your examples, 0.0000442615029219009
is less than 0.0001
and so is formatted using scientific notation. But 0.000442615029219009
is greater than 0.0001
and so gets formatted using fixed notation.
If you want your output always to use scientific notation then use Format
with the e
format string.
QC#107388
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With