Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Which is the best to use FloatToStrF or FormatFloat or Format ?


This question is answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Apr 13, 2018 6:33 AM Last Post By: Remy Lebeau (Te...
Mark Richards

Posts: 42
Registered: 12/8/99
Which is the best to use FloatToStrF or FormatFloat or Format ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2018 3:41 AM
I have been asked to update a c++ builder project and one .cpp has the following three mtehods to do the same thing in a single section of code. This got me to wonder which of the methods used is actually the fastest or best to use...


float fVal;
 
// txtAvg1, txtAvg2 and txtAvg3 are all (VCL) TEdit components
 
txtAvg1->Text = Sysutils::FloatToStrF(fVal, ffFixed, 7, 2);
 
txtAvg2->Text = Sysutils::FormatFloat("0.00", fVal);
 
txtAvg3->Text = Sysutils::Format("%f", ARRAYOFCONST((fVal)));
 


Can anyone suggest the most efficient of these methods?

Cheers! Marko
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Which is the best to use FloatToStrF or FormatFloat or Format ?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2018 5:36 AM   in response to: Mark Richards in response to: Mark Richards
El 13/04/2018 a las 12:41, Mark Richards escribió:
I have been asked to update a c++ builder project and one .cpp has the following three mtehods to do the same thing in a single section of code. This got me to wonder which of the methods used is actually the fastest or best to use...

 
float fVal;
 
// txtAvg1, txtAvg2 and txtAvg3 are all (VCL) TEdit components
 
txtAvg1->Text = Sysutils::FloatToStrF(fVal, ffFixed, 7, 2);
 
txtAvg2->Text = Sysutils::FormatFloat("0.00", fVal);
 
txtAvg3->Text = Sysutils::Format("%f", ARRAYOFCONST((fVal)));
 


Can anyone suggest the most efficient of these methods?

Cheers! Marko

The FloatToStrF function must be the fastest because it does not have to scan a string to determine the output format of
the real number.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Which is the best to use FloatToStrF or FormatFloat or Format ?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2018 6:33 AM   in response to: Mark Richards in response to: Mark Richards
Mark Richards wrote:

This got me to wonder which of the methods used is actually
the fastest or best to use...

Use a profiler and decide for yourself.

There is also String::sprintf():

txtAvg1->Text = String().sprintf(_D("%0.00f"), fVal);


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02