Logo Search packages:      
Sourcecode: icu version File versions  Download package

FieldPosition Class Reference

#include <fieldpos.h>

Inheritance diagram for FieldPosition:
Collaboration diagram for FieldPosition:

List of all members.

Public Types

enum  { DONT_CARE = -1 }

Public Member Functions

FieldPositionclone () const
 FieldPosition ()
 FieldPosition (int32_t field)
 FieldPosition (const FieldPosition &copy)
int32_t getBeginIndex (void) const
virtual UClassID getDynamicClassID () const
int32_t getEndIndex (void) const
int32_t getField (void) const
UBool operator!= (const FieldPosition &that) const
FieldPositionoperator= (const FieldPosition &copy)
UBool operator== (const FieldPosition &that) const
void setBeginIndex (int32_t bi)
void setEndIndex (int32_t ei)
void setField (int32_t f)
virtual ~FieldPosition ()

Static Public Member Functions

static UClassID U_EXPORT2 getStaticClassID ()
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW

Private Attributes

int32_t fBeginIndex
int32_t fEndIndex
int32_t fField

Detailed Description

FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted output. Fields are identified by constants, whose names typically end with _FIELD, defined in the various subclasses of Format. See ERA_FIELD and its friends in DateFormat for an example.

FieldPosition keeps track of the position of the field within the formatted output with two indices: the index of the first character of the field and the index of the last character of the field.

One version of the format method in the various Format classes requires a FieldPosition object as an argument. You use this format method to perform partial formatting or to get information about the formatted output (such as the position of a field).

The FieldPosition class is not suitable for subclassing.

Below is an example of using FieldPosition to aid alignment of an array of formatted floating-point numbers on their decimal points:

 
       double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789,
                  12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
       int dNumSize = (int)(sizeof(doubleNum)/sizeof(double));
       
       UErrorCode status = U_ZERO_ERROR;
       DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
       fmt->setDecimalSeparatorAlwaysShown(true);
       
       const int tempLen = 20;
       char temp[tempLen];
       
       for (int i=0; i<dNumSize; i++) {
           FieldPosition pos(NumberFormat::INTEGER_FIELD);
           UnicodeString buf;
           char fmtText[tempLen];
           ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
           for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces
           temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0';
           cout << temp << fmtText   << endl;
       }
       delete fmt;

The code will generate the following output:

 
           123,456,789.000
           -12,345,678.900
             1,234,567.880
              -123,456.789
                12,345.678
                -1,234.567
                   123.456
                   -12.345
                     1.234

Definition at line 106 of file fieldpos.h.


The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index