I usually print numbers with a specific number of decimal digits, but I now need to print one with a specified number of significant digits. The numbers need to be like "1.23" not like "1.2 E0". I see that Delphi will do the latter with the G format, but I cannot see how to do this in the former format.
Asked
Active
Viewed 2,802 times
2
-
1How do you _print_ the numbers? show some code! – jachguate Jan 11 '13 at 09:24
3 Answers
3
Use Format:
Format('%.3g',[Value]));
The 'g' specifies significant figures, I'll let you work out what the 3 means...

Steve Magness
- 863
- 14
- 25
0
FormatFloat('0.000', YourFloatNumber)
Use as many zeros as you would need to represent the digits.

gustavogbc
- 695
- 11
- 33
-
1'significant digits' does not specify a fixed number of decimals. Here's a link if you'd like to learn more: https://en.wikipedia.org/wiki/Significant_figures – Bjarke Moholt Oct 04 '15 at 13:06
0
Format('%.2g', MyNumber)
=> limit the output to 2 decimals
FormatFloat('0.00', MyNumber)
=> force exactly 2 decimals
Example:
MyNumber1 := 2
MyNumber2 := 3.8
MyNumber3 := 4.97
WriteLn(Format('Variant 1: %.2g / %.2g / %.2g', [MyNumber1, MyNumber2, MyNumber3]));
WriteLn(Format('Variant 2: %s / %s / %s', [FormatFloat('0.00', MyNumber1), FormatFloat('0.00', MyNumber2), FormatFloat('0.00', MyNumber3)]));
Output:
Variant 1: 2 / 3.8 / 4.97
Variant 2: 2.00 / 3.80 / 4.97

Michael Hutter
- 1,064
- 13
- 33