<< Return to Main Index

< Return to Class Index

CGXStyle::SetFormat

CGXStyle& SetFormat(unsigned ui);

ui

Specifies the format type. See below.

Return Value

Returns the this pointer.

Remarks

Objective Grid lets you format number values for static cells and specify the precision of the value (number of significant digits). The formatted value will be displayed only for static cells. If you start editing a cell the original value will be displayed.

For example, if you store the value 122.2345 into a cell and set the format to be fixed with two decimals,  122.23 will be displayed in the cell. When you click into the cell the cell value will change to 122.2345. Once you leave the cell the display text will change back to 122.2345.

When you apply numeric formatting to a cell it will only affect the cell if the value type is GX_VT_NUMERIC (see SetValueType).

The formatting of numeric cells is done with the GXFormatText method.

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.
Objective Grid uses SetNegativeZeroFlag(BOOL bDisplayNegativeZero = TRUE) to allow the end-user 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 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 ONLY applied when the number displayed is ALL zeroes.
If we change the above example's value to 0.00005 or higher, with the same precision of 4, the number displayed will be 0.0001.

Objective Grid supports the following formats. For certain format types you can also specify the number of places (refererred to as 'N' in the following table).

FormatId 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_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 $ 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-91.
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-91.
GX_FMT_DATE system Displays the integer portion of a date/time value in the format specified by the system settings.
GX_FMT_HIDDEN Hidden The cell contents are not displayed.
GX_FMT_TIME system Displays the fractional portion of a date/time value in the format specified by the system settings.
GX_FMT_PERCENT Percent Display 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 which contains 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.1991.
GX_FMT_ISO8061_DATE YYYY-MM-DD Displays the integer portion of a date/time value in the ISO 8061 date format 1991-08-01.
GX_FMT_DATETIME MM/DD/YY HH:MM Displays the date and time according to the system settings.
GX_FMT_TIME_HM_AMPM HH:MM AM/PM Displays the time only portion of a date/time in the format11:03 AM
GX_FMT_TIME_HMS_AMPM HH:MM:SS AM/PM Displays the time only portion of a date/time in the format11:03:15 AM
GX_FMT_TIME_HM HH:MM Displays the time only portion of a date/time in the format13:03
GX_FMT_FIXED_PARENS (fixed) Displays the number using a fixed number of decimal places, specified by N, with negative values enclosed in parentheses.
GX_FMT_MIXED_FRACTIONS mixed fraction Displays the number as a mixed fraction (a whole number followed by a proper fraction). The value N specifies the maximum number of digits for the denominator of the fractional part. N is restricted to values between 1 and 4.
GX_FMT_USER1 User1 if you override GXFormatText you might use
GX_FMT_USER2 User2 these format codes (GX_FMT_USER1 .. GX_FMT_USER4)
GX_FMT_USER3 User3 for your own currenct formatting (e.g. DM, Lira etc).
GX_FMT_USER4 User4

Use CGXStyle::SetFormat to apply number formatting to cells.

Example:

      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));

See Also

CGXStyle::SetValueType CGXStyle::SetPlaces CGXStyle::SetNegativeZeroFlag GXFormatText

CGXStyle

Class Overview | Class Members