Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2004-2013 Curt Hill Formatting Reals Outputs other than normal.

Similar presentations


Presentation on theme: "Copyright © 2004-2013 Curt Hill Formatting Reals Outputs other than normal."— Presentation transcript:

1 Copyright © 2004-2013 Curt Hill Formatting Reals Outputs other than normal

2 Copyright © 2004-2013 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

3 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 © 2004-2013 Curt Hill

4 The Solutions There are two static methods of the wxString class –Format –FromDouble We will look at both Copyright © 2004-2013 Curt Hill

5 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 © 2004-2013 Curt Hill

6 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 © 2004-2013 Curt Hill

7 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 © 2004-2013 Curt Hill

8 Example Consider the following code: Memo1->AppendText( wxString::FromDouble(d,8)) << wxString::FromDouble(d,2)); Suppose that d has 128.5 Then the result would be 128.50000000 and 128.50 Copyright © 2004-2013 Curt Hill

9 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 © 2004-2013 Curt Hill

10 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 © 2004-2013 Curt Hill

11 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 © 2004-2013 Curt Hill

12 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 © 2004-2013 Curt Hill

13 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 © 2004-2013 Curt Hill

14 Example Consider the following code: Memo1->AppendText( wxString::Format("%8.2f",d)); Suppose that d has a value of 21.737 then it will display as 21.74 with three leading blanks Notice the rounding that occurred Copyright © 2004-2013 Curt Hill

15 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 © 2004-2013 Curt Hill

16 Example Consider: wxString s = s.Format( “A is %5.2f and B is %5d”, A, B); If A is 3.476 and B is 12 we would get: A is 3.48 and B is 12 Copyright © 2004-2013 Curt Hill

17 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 © 2004-2013 Curt Hill

18 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 © 2004-2013 Curt Hill


Download ppt "Copyright © 2004-2013 Curt Hill Formatting Reals Outputs other than normal."

Similar presentations


Ads by Google