问题
I need to call Format function to return the following strings:
1.0 -> "1 or 1.0"
1.01 - > "1.01"
1.001 - > "1.001"
1.0000001 -> "1.0000001"
It it possible? The standard format string %0:f returns only two digits after the point (for example, "1.01").
回答1:
Use the %g general format string:
{$APPTYPE CONSOLE}
uses
System.SysUtils;
begin
Writeln(Format('%g', [1.0]));
Writeln(Format('%g', [1.01]));
Writeln(Format('%g', [1.001]));
Writeln(Format('%g', [1.0000001]));
end.
Output
1 1.01 1.001 1.0000001
来源:https://stackoverflow.com/questions/24562127/how-can-i-format-string-for-float-number-on-delphi-correctly