Archived

This topic is now archived and is closed to further replies.

Formatting a double in an edit box

This topic is 6347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would someone go about limiting a number in an edit box to 2 decimal places? The var attached to the edit box is a double, not a string, so I can''t use the string formatting functions...

Share this post


Link to post
Share on other sites
Can't you convert it to a string, format it too only have two decimals and then convert it back to a double?

Edited by - Muzzafarath on September 1, 2000 11:42:40 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I thought about that.. however.. what about the "." when converting to a string, won''t that get translated to something else?

Share this post


Link to post
Share on other sites
Why would the dot get converted to something else...? This will _probably_ work:

        
double d = Get_Double();
char buffer[512]
_gcvt(d, 512, buffer);

sprintf(buffer, "%.2f", buffer);

d = atof(buffer);


I think _gcvt is MS VC++ specific, but I'm sure there's a function that converts a double to string in your compiler too.

Edited by - Muzzafarath on September 1, 2000 11:53:17 AM

Share this post


Link to post
Share on other sites
Use atof() and ftoa() and you won''t have any trouble.

------------------------------
#pragma twice

Share this post


Link to post
Share on other sites
There's no ftoa in VC++ or DJGPP... Where have you seen that function?

Edited by - Muzzafarath on September 1, 2000 12:03:00 PM

Share this post


Link to post
Share on other sites
Correct me if I'm wrong, but I don't think 'ftoa' is in the ansi standard... strtod & atof are though (but go the wrong way)

And for what it's worth, Muzzafarath's code should really go:

                
sprintf(buffer, "%.2f", d);
d = atof(buffer);


There's no point doing two conversions when sprintf will do it for you.

You can get the same effect with d = floor(d*100.0)/100.0; Though I've no idea how it's going to help you limit the input to two dp's...

Add some sort of input filter?

Jans.

-----------------
Janucybermetaltvgothmogbunny


Edited by - Jansic on September 1, 2000 12:22:20 PM

Share this post


Link to post
Share on other sites