?
Solved

FormatFloat  in Delphi

Posted on 2011-05-08
10
Medium Priority
?
2,006 Views
Last Modified: 2013-11-23
How to use FormatFloat  in Delphi
0
Comment
Question by:dabbiX
9 Comments
 
LVL 25

Accepted Solution

by:
epasquier earned 1336 total points
ID: 35716516
it's all in Delphi Help :

function FormatFloat(const Format: string; Value: Extended): string;

ex: Str:=FormatFloat('0.00', 31.678); => '31.67'

Please advise if there is a specific need or thing you don't understand in the format string
0
 

Author Comment

by:dabbiX
ID: 35716602
How to use FormatFloat  in Delphi
ok
in memo;

 Edit2.Text :=FormatFloat('#,##0.00',debet);
 Edit3.Text :=FormatFloat('#,##0.00',kredit);
 memo1.Lines.Add('d.. '+Edit2.Text);
 memo1.Lines.Add('k.. '+Edit3.Text);

How to get  Algin :=  Right ;
0
 
LVL 25

Expert Comment

by:epasquier
ID: 35716726
easiest way : you set that in the memo option
memo1.Alignment := taRightJustify;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:dabbiX
ID: 35716760
memo1.Alignment := taRightJustify;

that is no way.

I want memo1.Lines.Add('d.. '+Edit2.Text);

to by RightJustify
0
 

Author Comment

by:dabbiX
ID: 35716787
s :=FormatFloat('#,##0.00',debet);
s1 :=FormatFloat('#,##0.00',debet);

memo1.Lines.Add('d.. '+s);
memo1.Lines.Add('d.. '+s1);

s and s1      to by  =    RightJustify

0
 
LVL 25

Assisted Solution

by:epasquier
epasquier earned 1336 total points
ID: 35716906
ok, then use Format function instead

memo1.Lines.Add(Format('d.. %10.2f',[debit]));

%10.2f has the following signification :
'f' : format the parameter as fixed decimal value
10 (first value between % and f) : the whole parameter will take AT LEAST 10 characters, filled with spaces if need be, and right aligned by default (left aligned if preceded with '-')
2 (second value, after a '.') : precision, or nb of digits after the decimal

only inconvenience of Format function used that way : no option to display thousand separators.

How to display thousand separators anyway : use of BOTH Format and FormatFloat this way :
s :=FormatFloat('#,##0.00',debet);
memo1.Lines.Add(Format('d.. %10s',[s]));

now, the format string use %s parameter, which stands for STRING, the 10 value between % and 's' saying as above to RIGHT-ALIGN, padding with spaces, for a result of at least 10 characters
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 664 total points
ID: 35718618
if you want thousandseparators ... use n instead of f with Format

on top of that ... if you want to use a variable for the width alignment :
assuming 80 characters wide:

this is a sample using locale formatsettings of the user and altering a few
and then passing those new settings on to format
function FloatFormat(aFloat: double; aPrecision: integer = 2; aWidth: integer = 10): string;
var fmt: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, fmt);
  fmt.ThousandSeparator := '_';
  fmt.DecimalSeparator := '.';
  Result := Format('%.*n', [aPrecision, aFloat], fmt);
  if aWidth > Length(Result) then
    Result := Format('%*.*s', [aWidth, aWidth, Result]);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(FloatFormat(500200.300500, 3, 80));
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 35805917
Hi Dabby !

Did we gave you enough info about how to use Format functions ? There is plenty more information in Delphi Help.

LocaleFormatSettings can give you also a lot of options to control how your application can format values (floats, dates, time etc..) but I recommend not playing too much with those, except if you really need setting specific display standards in your entire application. Playing too much with those global settings could well have an undesired effect on other components, some of which you maybe don't have the code.

PS : Thanks Geert for the %n tip, I have always overlooked that one. I don't use much thousand separators, but who knows, maybe one day I will need it !
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 36032600
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question