Jump to content
BruceV

Truncating precision of type double for GUI variables.

Recommended Posts

I would appreciate some advice on how to do the following. It seemed trivial, but I quickly got tangled up with the various formats for wide strings, a lot of the Embarcadero information turned out to be for Pascal, I’m working with C++ builder. And there are a number of different wide string formats.

 

I am displaying double values on the canvas, the following obviously works fine:

Label1 -> Caption = DoubleVal  ;

 

But I got into trouble when I wanted to constrain the display precision, there doesn’t appear to be a setting for the display object and just truncating the field width would be visually unappealing. So I applied the setprecision function with a stringstream output…..

 

ostringstream oss ;

oss << setprecision (4) << DoubleVal;  // This works

 

 How do I go from the stringstream to the GUI? Or is there a more direct approach?

Share this post


Link to post

You could use:

Label1->Caption = oss.str().c_str();

  • Like 2

Share this post


Link to post

You could also use FloatToStrF() which returns a UnicodeString using the local decimal separator.

In case you want get a double value from an input text, you could use TryStrToFloat() which returns TRUE if the input is a valid floating point number (which avoids an exception in case the input text is invalid).

Share this post


Link to post
7 hours ago, hansw said:

You could also use FloatToStrF() which returns a UnicodeString using the local decimal separator.

Oh yeah, I keep forgetting about that one.

 

Label1->Caption = FloatToStrF(DoubleVal, ffFixed, 10, 4);

 

There is also FormatFloat():

 

Label1->Caption = FormatFloat(_D("0.0000"), DoubleVal);

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×