Recent

Author Topic: Converting Single to String  (Read 2793 times)

nikel

  • Sr. Member
  • ****
  • Posts: 267
Converting Single to String
« on: June 14, 2019, 05:49:38 pm »
Hi, I'm trying to output version of my program. I created a single variable, here's my code:

Code: Pascal  [Select][+][-]
  1. private
  2.   Ver: Single;
  3.   ...
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   Ver:=1.30;
  7.   try
  8.     ShowMessage(FormatFloat('#.##', Ver));
  9.   except
  10.     on E: Exception do
  11.       ShowMessage(E.Message);
  12.   end;
  13.   ...

This outputs 1,3
How can I convert it to show 1.30?
« Last Edit: June 14, 2019, 07:30:41 pm by nikel »

wp

  • Hero Member
  • *****
  • Posts: 13361
Re: Converting Single to String
« Reply #1 on: June 14, 2019, 06:02:04 pm »
Use '#.00' or '0.00' as format mask. Note that the first version suppresses a leading zero, i.e. when the number is 0.1 the output string will be '.10'; when you need the leading zero in this case use '0.00' where 0.1 will be shown as '0.10'.

ASerge

  • Hero Member
  • *****
  • Posts: 2475
Re: Converting Single to String
« Reply #2 on: June 15, 2019, 04:06:12 am »
Use '#.00' or '0.00' as format mask. Note that the first version suppresses a leading zero, i.e. when the number is 0.1 the output string will be '.10'; when you need the leading zero in this case use '0.00' where 0.1 will be shown as '0.10'.
Addition: if you need is a dot as separator, then:
Code: Pascal  [Select][+][-]
  1. var
  2.   FS: TFormatSettings;
  3. begin
  4.   FS := DefaultFormatSettings;
  5.   FS.DecimalSeparator := '.';
  6.   ShowMessage(FormatFloat('0.00', Ver, FS));

 

TinyPortal © 2005-2018