Delphi/Formatfloat

Advertisement


Question
QUESTION: Hi Randy,

At one point- my program either multiplies or divides (User's choice) two real numbers and then displays the result in an edit box. Formatfloat is used like this to show two decimal places

Edit1,text:=Formatfloat('#,##0.00',mydouble);

The code to do this is contained in a button's onclick event.
The Edit box displays the result properly the first time the button is clicked and usually the second time as well. further clicks make the display show 3 decimal places and more.
I can understand the number before the decimal point increasing but I thought that the formatting would prevent the display showing more than 2 decimal places.
Hope I've explained this properly and if so Can it be cured?

Thanks

Michael

ANSWER: Michael,
 I tried to review the source code for FormatFloat, but it calls an assembly routine, and I don't have the patience to debug it... This is the routine that I use to impersonate that same output:

function  RtoC(Source: Double; S,D: integer; ZeroSuppress: Boolean=False): string;
Var TStr: string;
begin
 TStr:=Format('%'+IntToStr(S)+'.'+IntToStr(D)+'n',[Source]);
 if ZeroSuppress and (Source=0) then TStr:=Spc(S);
 RtoC:= TStr;
end;

This is how you would use it in that case:

Edit1.text:=RtoC(mydouble,0,2);

The S parameter specifies the padding. Zero padding does not add any padding to the text. The D parameter specifies the number of decimal places.
Sincerely,
Brian Sill

---------- FOLLOW-UP ----------

QUESTION: Thanks Brian,

I have a few Delphi versions on my Computers and unfortunately the program I need

help on is in a Delphi 2 timewarp.  That's because it contains some third party

components in DCU form only  so I can't upgrade it.

The Delphi 2 compiler complains about boolean variable being initialised in your

Functions declaration although I suppose that can be overcome.
It also doesn't recognise the SPC function used.
Can this be modified to work in D2?

Michael

Answer
Michael,
if that's the case, I'd reduce the function to

function  RtoC(Source: Double; S,D: integer): string;
Var TStr: string;
begin
TStr:=Format('%'+IntToStr(S)+'.'+IntToStr(D)+'n',[Source]);
RtoC:= TStr;
end;

The only thing you loose is that if the number is zero, it will output 0.00 instead of having the option of an empty string when the number is zero.
Sincerely,
Brian

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Brian Sill

Expertise

Designing and implementing DOS, DPMI, and Windows apps in TP, BP, and Delphi (Windows VCL, Custom Controls, Runtime UI design, Windows API, Database, SQL, Threading, Btrieve, Pervasive.SQL, Advantage Database, ADO to MDB, ADO to MS SQL, MDI Apps, MS Office Integration, barcode generation, Document imaging application development, TIFF file structure, and some IFF file experience).

Experience

Turbo Pascal and Delphi developer since 1986. Proficient in web design incuding XHTML, CSS, and Javascript. Fluent in C, C++, Visual Basic (especially VBA), and Modula-2.

Education/Credentials
Over 20 years designing and implementing DOS, DPMI, and Windows apps in TP, BP, and Delphi.

©2016 About.com. All rights reserved.