FloatToStrF function
Convert a float value to a string using a given format.
Declaration
function FloatToStrF(Value: Extended; format: TFloatFormat;
Precision: Integer; Digits: Integer): stringDescription
FloatToStrF converts the floating point number
value to a string
representation, according to the settings of the parameters
Format,
Precision and
Digits.
The meaning of the Precision and Digits parameter depends on the
Format parameter. The format is controlled mainly by the Format
parameter. It can have one of the following values:
- ffCurrency
- Money format.
Valueis converted to a string using the global variablesCurrencyString,CurrencyFormatandNegCurrFormat. TheDigitsparameter specifies the number of digits following the decimal point and should be in the range -1 to 18. If Digits equals-1,CurrencyDecimalsis assumed. ThePrecisionparameter is ignored. - ffExponent
- Scientific format.
Valueis converted to a string using scientific notation: 1 digit before the decimal point, possibly preceded by a minus sign ifValueis negative. The number of digits after the decimal point is controlled byPrecisionand must lie in the range 0 to 15. - ffFixed
- Fixed point format.
Valueis converted to a string using fixed point notation. The result is composed of all digits of the integer part ofValue, preceded by a minus sign ifValueis negative. ifDigitsis positive, then following the integer part isDecimalSeparatorand then the fractional part ofValue, rounded off toDigitsnumbers. If the number is too large then the result will be in scientific notation. - ffGeneral
- General number format. The argument is converted to a
string using
ffExponentorffFixedformat, depending on which one gives the shortest string. There will be no trailing zeroes. IfValueis less than0.00001or if the number of decimals left of the decimal point is larger thanPrecisionthen scientific notation is used, andDigitsis the minimum number of digits in the exponent. OtherwiseDigitsis ignored. The minimum precision is 2. - ffNumber
- Is the same as
ffFixed, except that thousand separators are inserted in the resulting string.
Errors
None.
See also
Example: ex68
Program Example68;
{ This program demonstrates the FloatToStrF function }
Uses sysutils;
Const Fmt : Array [TFloatFormat] of string[10] =
('general','exponent','fixed','number','Currency');
Procedure Testit (Value : Extended);
Var I,J : longint;
FF : TFloatFormat;
begin
For I:=5 to 15 do
For J:=1 to 4 do
For FF:=ffGeneral to {ffCurrency} ffGeneral do
begin
Write (Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : ');
Writeln (FloatToStrF(Value,FF,I,J));
Write (-Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : ');
Writeln (FloatToStrF(-Value,FF,I,J));
end;
end;
procedure Test2;
var
F : Double;
begin
F:=0.99411;
Writeln('0,0: ',floattostrF(F,FFgeneral,0,0));//=> 0.99,
Writeln('1,0: ',floattostrF(F,FFgeneral,1,0));//=> 0.99
Writeln('2,0: ',floattostrF(F,FFgeneral,2,0));//=> 0.99
Writeln('3,0: ',floattostrF(F,FFgeneral,3,0));//=> 0.994
F:=0.99511;
Writeln('0,0: ',floattostrF(F,FFgeneral,0,0));//=> 1
Writeln('1,0: ',floattostrF(F,FFgeneral,1,0));//=> 1
Writeln('2,0: ',floattostrF(F,FFgeneral,2,0));//=> 1
Writeln('3,0: ',floattostrF(F,FFgeneral,3,0));//=> 0.995
F:=12.145;
Writeln('0,0: ',floattostrF(F,FFgeneral,0,0));//=> 12
Writeln('1,0: ',floattostrF(F,FFgeneral,1,0));//=> 12
Writeln('2,0: ',floattostrF(F,FFgeneral,2,0));//=> 12
Writeln('3,0: ',floattostrF(F,FFgeneral,3,0));//=> 12.1
Writeln('4,0: ',floattostrF(F,FFgeneral,4,0));//=> 12.15
Writeln('5,0: ',floattostrF(F,FFgeneral,5,0));//=> 12.145
end;
Begin
Testit (1.1));
Testit (1.1E1));
Testit (1.1E-1));
Testit (1.1E5));
Testit (1.1E-5));
Testit (1.1E10));
Testit (1.1E-10));
Testit (1.1E15));
Testit (1.1E-15));
Testit (1.1E100));
Testit (1.1E-100));
End.An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
