TextFloatMethods (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example reads a floating-point value as a string and calls the TextToFloat method that will convert the number to a floating-point value. Then, you modify the decimal separator and call the FloatToText method to convert the floating-point value to a string that will be further displayed.

Code

var
  LText: String;
  LNumber: Double;

begin
  { Read the floating-point value as a string. }
  Write('Enter a floating point number: '); Readln(LText);

  { Call the TextToFloat method to convert the string value to a double. }
  TextToFloat(PChar(LText), LNumber, fvExtended);

  { Output the results to the console. }
  Writeln('The number with the standard decimal separator is: ', LText);

  { Now modify the separator character. }
  DecimalSeparator := ',';

  { Set the length of the incoming buffer to 64 chars (max). }
  SetLength(LText, 64);

  { Call FloatToText with the value passed as an Extended, with precision 15.
    The result of the call is the number of characters actually set in the buffer
    (LText). Use this length to reset the length of the buffer. }
  SetLength(LText, FloatToText(PWideChar(LText), LNumber, fvExtended, ffGeneral, 15, 0));

  { Output the results to the console. }
  Writeln('The number with the modified decimal separator is: ', LText);
  Readln;
end.

Uses