Objective Grid : PART I User’s Guide : Chapter 5 Using Objective Grid : Number Formatting
Number Formatting
 
Value Types
CGXStyle::SetValueType() allows you to specify the value type in a cell. Possible types are:
GX_VT_STRING
GX_VT_NUMERIC
GX_VT_EXPRESSION
GX_VT_ERROR
CGXStyle::GetValueType() returns the value type of the cell.
SetValueType() does not affect the way values are stored in CGXStyle. All values are stored as a string. SetValueType() only offers additional information about the value that is stored in the cell. For example, when you apply numeric formatting to a cell, it will only affect the cell if its value type is GX_VT_NUMERIC.
The value type information is very important when you pass style objects to the formula engine. The formula engine parses values to determine if they should be stored as strings or as numbers. It is very important for the formula engine to distinguish between numbers and strings.
 
// Sets the value type to GX_VT_NUMERIC
SetValueRange(CGXRange(nRow, nCol), 122.22);
 
// Sets the value type to GX_VT_STRING
SetValueRange(CGXRange(nRow, nCol), "122.22");
 
// Sets the value type to GX_VT_STRING
SetStyleRange(CGXRange(nRow, nCol),
CGXStyle().SetValue ("122.22"));
 
// Sets the value type to GX_VT_NUMERIC
SetStyleRange(CGXRange(nRow, nCol), CGXStyle().SetValue (929.2));
 
// SetExpressionRowCol parses the string and sets the
// value type to GX_VT_NUMERIC
SetExpressionRowCol (nRow, nCol, "1222.22");
 
// SetExpressionRowCol parses the string and sets the
// value type to GX_VT_STRING
SetExpressionRowCol (nRow, nCol, "ABDGDDG1222.22");
Formatting Number Values for Static Cells
The following methods pertain to number formatting:
CGXStyle::SetFormat()
CGXStyle::SetPlaces()
CGXStyle::SetNegativeZeroFlag()
These methods let you format number values for static cells and specify the precision of the value (the number of significant digits).
Objective Grid lets you format number values for inactive cells and specify the precision when appropriate. The formatted value is displayed only for static (inactive) cells. If you start editing a cell, the original value is displayed.
For example, if you store the value 122.2345 in a cell and then set the format as fixed with two decimals, 122.23 is displayed in the cell. When you click the cell make the cell active, the cell value changes to 122.2345. Once the cell is nactive again, the display text reverts to 122.23.
When you apply numeric formatting to a cell, it affects the cell only if the value type is GX_VT_NUMERIC.
Objective Grid supports the following formats. For certain format types, you can also specify the number of places (referred to as 'N' in Table 8).
Table 8 – Format Types 
Format ID
Description
Definition
GX_FMT_FLOAT
Scientific
Displays the number in scientific notation (exponent form) with N significant digits.
GX_FMT_FIXED
Fixed
Displays the number using a fixed number of decimal places, specified by N.
GX_FMT_FIXED_PARENS
Fixed
Displays the number using a fixed number of decimal places, specified by N. Uses parentheses to indicate negative numbers.
GX_FMT_NUMLOCALE
Locale
Displays number using locale from regional settings ( , instead of . for example).
GX_FMT_GEN
General
Displays the number in fixed format or scientific notation, whichever fits. Trailing zeros are not displayed.
GX_FMT_DOLLARS
Dollars
Displays the number with a leading dollar sign ($) and with comma delimiters, as in $1,000,000. Negative values are displayed in parentheses.
GX_FMT_COMMA
Comma
Displays the number with comma delimiters, as in 1,000,000. Negative values are displayed in parentheses.
GX_FMT_HEX
Hex
Display the number in hexadecimal notation. For example, the value 31 is displayed as 1F.
GX_FMT_LOGIC
Logic
Displays the value 0 as 0, the value 1 as 1, and all other values as ?.
GX_FMT_DAY_MONTH_YEAR
DD-MMM-YY
Displays the integer portion of a date/time value as a Gregorian date, in the format 01-Aug-99.
GX_FMT_DAY_MONTH
DD-MMM
Displays the integer portion of a date/time value in the format 01-Aug.
GX_FMT_MONTH_YEAR
MMM-YY
Displays the integer portion of a date/time value in the format Aug-99.
GX_FMT_DATE
MM/DD/YY
Displays the integer portion of a date/time value in the format 08/01/99.
GX_FMT_HIDDEN
Hidden
The cell contents are not displayed.
GX_FMT_TIME
HH:MM:SS
Displays the fractional portion of a date/time value in the format 06:15:30.
GX_FMT_PERCENT
Percent
Displays the number as a percentage, multiplying it by 100. For example, the value 0.1 is displayed as 10.00%.
GX_FMT_TEXT
Text
For cells that contain formulas, the formula itself is displayed rather than the computed value of the cell.
GX_FMT_INTL_DATE
DD.MM.YYYY
Displays the integer portion of a date/time value in the format 01.08.1999.
GX_FMT_ISO8061_DATE
YYYY-MM-DD
Displays the integer portion of a date/time value in the ISO 8061 date format 1999-08-01.
GX_FMT_DATETIME
MM/DD/YY HH:MM
Displays the date and time according to the system settings.
GX_FMT_USER1
User1
If you override GXFormatText you might use these format codes:
GX_FMT_USER2
User2
(GX_FMT_USER1, GX_FMT_USER2
GX_FMT_USER3
User3
GX_FMT_USER3, GX_FMT_USER4)
GX_FMT_USER4
User4
for your own currency formatting (e.g. DM, Lira, ...).
Use CGXStyle::SetFormat() to apply number formatting to cells.
 
SetExpressionRowCol(nRow, 1, _T("General"));
SetExpressionRowCol(nRow+1, 1, _T("7777.77"));
SetStyleRange(CGXRange(nRow+1, 1),
CGXStyle().SetFormat(GX_FMT_GEN).SetPlaces(15));
 
SetExpressionRowCol(nRow, 2, _T("Fixed"));
SetExpressionRowCol(nRow+1, 2, _T("7777.77"));
SetStyleRange(CGXRange(nRow+1, 2),
CGXStyle().SetFormat(GX_FMT_FIXED).SetPlaces(4));
 
SetExpressionRowCol(nRow, 3, _T("Scientific"));
SetExpressionRowCol(nRow+1, 3, _T("7777.77"));
SetStyleRange(CGXRange(nRow+1, 3),
CGXStyle().SetFormat(GX_FMT_FLOAT).SetPlaces(4));
 
SetExpressionRowCol(nRow, 4, _T("Dollars"));
SetExpressionRowCol(nRow+1, 4, _T("7777.77"));
SetStyleRange(CGXRange(nRow+1, 4),
CGXStyle().SetFormat(GX_FMT_DOLLARS).SetPlaces(2));
 
SetExpressionRowCol(nRow, 5, _T("Comma"));
SetExpressionRowCol(nRow+1, 5, _T("7777.77"));
SetStyleRange(CGXRange(nRow+1, 5),
CGXStyle().SetFormat(GX_FMT_COMMA).SetPlaces(2));
 
SetExpressionRowCol(nRow, 6, _T("Percent"));
SetExpressionRowCol(nRow+1, 6, _T(".77"));
SetStyleRange(CGXRange(nRow+1, 6),
CGXStyle().SetFormat(GX_FMT_PERCENT).SetPlaces(2));
Suppressing the Display of Negative Values for Zero
By default, when using the GX_FMT_FIXED format type only, Objective Grid displays 0.0, with any precision, as negative. This is not always desirable.
Use SetNegativeZeroFlag(BOOL bDisplayNegativeZero = TRUE) to change the default of TRUE to FALSE.
For example, if you store the value 0.00004 with a precision of 4 using SetPlaces(4), the number displayed is -0.0000 by default.
However, if you set SetNegativeZeroFlag(FALSE), the number is displayed without the negative, as 0.0000.
Here is an example, as written in a Grid application's View.cpp file in Grid's OnIntialUpdate() function:
 
SetStyleRange(CGXRange(nRow, nCol),
CGXStyle().SetValue("-0.00004").SetValueType(GX_VT_NUMERIC).
SetFormat(GX_FMT_FIXED).SetNegativeZeroFlag(FALSE));
Also note that this formatting is applied only when the number displayed is all zeros.
If we change the above example's value to 0.00005 or higher with the same precision of 4, the number displayed is 0.0001.
This functionality is also available in Objective Grid's CGXPropertySheet-derived class CGXStyleSheet as well as in Grid's resource files gxres.rc, gxresdeu.rc, gxresfra.rc and gxresnld.rc. The relevant check box is Display Negative Zero? which is checked by default on the Format property sheet page.
For an example of this usage, see the updated GridApp sample. Simply run the sample, select GRIDAP document from the New dialog window, select the Format menu option, then Cells... and then select the Format tab in the Cells dialog. The Display Negative Zero check box or one of its shipped translations, displays on the right below the Precision edit box.
Recognizing Locale Settings in Static Formatting
NOTE >> This applies only to non formula grids.
By default, Objective Grid does not use regional settings when you choose to format numbers. So, it cannot understand that 1.345,13 is a number, and format it properly, even if your system's regional settings are set so that period is the thousands separator and comma is the decimal separator. To get Objective Grid to use your regional settings, add a call to CGXGridCore:: EnableLocaleNumbers() in your grid's Initialize() or OnInitialUpdate() call. After that, any cell using a CGXEditControl that has been formatted using one of the codes listed below will use the numeric locale settings. In addition, with this setting enabled, Objective Grid will recognize values typed into numeric formatted cells as being numerical values.
No matter what the locale settings, the actual numerical values stored in the grid's data object use a period as the decimal separator and do not contain any thousands separator. This convention makes the grid's serialization locale independent. Numbers serialized from a grid using Italian settings can be read into a grid using English settings without losing the numerical values.
Also, for historical reasons, Objective Grid catches system changes through handling the WM_WININICHANGE message. If your code implements a handler for the newer WM_SETTINGCHANGE message, make sure you return a FALSE in your message handler so the grid's handler will also be called.
To customize this behavior, derive your control from CGXEditControl and override one of these three virtual CGXEditControl methods.
 
virtual BOOL StripSeparatorFromValue(CString& sValue,
const CGXStyle* pStyle, double& d);
// called from Store & ConvertControlTextToValue (handles Paste)
// to strip group separator from the value before it is stored
 
virtual BOOL PutDecInValue(CString& sValue, const CGXStyle* pStyle);
//called from Init to put correct dec sep in windowtext
virtual BOOL AcceptCommasInNumbers(const CGXStyle* pStyle);
// returns whether strings with thousands separator should be
// treated as a number. Default behavior checks for one of these
// number formats:
// GX_FMT_FIXED
// GX_FMT_COMMA
// GX_FMT_DOLLARS
// GX_FMT_FLOAT
// GX_FMT_GEN
// GX_FMT_FIXED_PARENS
// GX_FMT_NUMLOCALE
By default in a non-formula grid, the code below will display 1234.5555 as 1,234.56 no matter what your regional settings are. But if you add a call to EnableLocaleNumbers() in your initialization code, the formatting will reflect your regional settings. For example, using Italian locale settings would display the value 1234.5555 as 1.234,56.
SetStyleRange(CGXRange(1,1,1,2), CGXStyle()
.SetFormat(GX_FMT_COMMA)
.SetValueType(GX_VT_NUMERIC)
.SetPlaces(2)
.SetValue((double) 1234.5555)
);