Formatting a double in an edit box
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...
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
Edited by - Muzzafarath on September 1, 2000 11:42:40 AM
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:
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
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
Use atof() and ftoa() and you won''t have any trouble.
------------------------------
#pragma twice
------------------------------
#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
Edited by - Muzzafarath on September 1, 2000 12:03:00 PM
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:
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
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
Popular Topics
Advertisement