Formatting a double in an edit box

Started by
5 comments, last by jaymz 23 years, 7 months ago
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...
Advertisement
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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
I thought about that.. however.. what about the "." when converting to a string, won''t that get translated to something else?
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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Use atof() and ftoa() and you won''t have any trouble.

------------------------------
#pragma twice
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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
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

This topic is closed to new replies.

Advertisement