Copyright © Curt Hill Formatting Reals Outputs other than normal
Copyright © Curt Hill Introduction C++ is a general purpose language The default behavior of anything must be general –Work for all types of applications –Scientific, business or whatever The general solution is never the right solution for all applications Something better is needed
What do we want? To be able to take a float or double and get more than just the standard output: –Control the number of digits of precision –Control digits to left and right of the decimal point We could write our own functions to do this Dev-C++ already has a mechanism for this Copyright © Curt Hill
The Solutions There are two static methods of the wxString class –Format –FromDouble We will look at both Copyright © Curt Hill
wxStrings There are several methods of wxString that will be useful to format floats and doubles These are static methods That means they do not need an instance of a wxString to work They do produce an output String Copyright © Curt Hill
Static Methods Both of these are static methods of wxString What is a static method? A method that does not need an instance of the variable of that type Thus we need to call it in one of two ways: –Using an uninvolved instance –Using the class name and scope resolution operator Copyright © Curt Hill
FromDouble Method signature: wx String FromDouble(double,int) The function returns a string representation of the first parameter The second is then number of digits following the decimal point Copyright © Curt Hill
Example Consider the following code: Memo1->AppendText( wxString::FromDouble(d,8)) << wxString::FromDouble(d,2)); Suppose that d has Then the result would be and Copyright © Curt Hill
Format Method signature: wx String Format(string,double) The function returns a string representation of the second parameter, based on the format code of the first parameter The second is the item to be formatted Format codes will be considered next Copyright © Curt Hill
More This scheme is an adaptation of how C did input and output with scanf and printf The first parameter was a format specifier –Quoted string –The format specifier started with a % and ended with the type specification The parameters that followed were plugged into the string into the specifiers Copyright © Curt Hill
Specifiers C had many specifiers but we only want to look at a couple of them A float or double could be handled with the f specifier –This used standard or fixed point notation An int could be handled with the d specifier There were also modifiers Copyright © Curt Hill
Lengths Between the % and the format descriptor can come a length For integers using %d –%d means leave no extra blanks –%4d means use at least 4 spaces –Any spaces will come at the front –If item needs more than 4 it will use it Copyright © Curt Hill
Floats and Doubles For floats and doubles use %f %f means standard default A constant may also be inserted –%12f This may also include a decimal –%12.5 means: –Entire width is 12 –Digits after decimal of 5 –The digit comes out of the 12 –If more are needed the 12 can be enlarged This is fixed decimal format Exponential format is with the %e Copyright © Curt Hill
Example Consider the following code: Memo1->AppendText( wxString::Format("%8.2f",d)); Suppose that d has a value of then it will display as with three leading blanks Notice the rounding that occurred Copyright © Curt Hill
Multiples Anything in the quoted string that was not a format specifier appeared as itself Multiple format specifiers could also be used if there were multiple parameters that followed They should match as to type Copyright © Curt Hill
Example Consider: wxString s = s.Format( “A is %5.2f and B is %5d”, A, B); If A is and B is 12 we would get: A is 3.48 and B is 12 Copyright © Curt Hill
Precision A float has at most about 7 decimal digits A double has at most about 15 decimal digits The length of the specifier should be less than or equal that maximum for the type Copyright © Curt Hill
Lastly FromDouble would be nice for seeing more digits than the standard Format is better when generating a column of numbers and decimal point alignment would be nice –Do not forget to use a monospaced font for this A demo exists, lets look at it Copyright © Curt Hill