com.informix.lang

## Class Decimal

- java.lang.Object
- - com.informix.lang.Decimal

    public class Decimal
    extends java.lang.Object

### Field Summary

Fields
`protected byte[]` `dec_dgts` Array of base 100 digits (2 per byte)
`protected short` `dec_exp` Exponent in base 100
`protected short` `dec_ndgts` Base 100 digits
`protected short` `dec_pos` Sign.
`static int` `DEC_T_SIZE` number of bytes in a C dec_t struct sizeof(short) * 3 + DECSIZE = 22
`static short` `DECPOSNEGATIVE` indicates NEGATIVE decimal number
`static short` `DECPOSNULL` indicates NULL decimal number
`static short` `DECPOSPOSITIVE` indicates POSITIVE decimal number
`static int` `DECSIZE` number of bytes allocated for IFX DECIMAL
`static java.lang.String` `digChar`
`protected byte` `endCode` endCode is the end DateTime code as received from IFX Only relevent when timestamp = true
`protected int` `lenOfFirstField`
`protected byte` `pdigs` Total significant digits.
`protected short` `precStored`
`protected byte` `pscale` Scale, i.e., digits after decimal point
`protected byte` `startCode` startCode is the start DateTime code as received from IFX Only relevent when timestamp = true
`protected boolean` `timestamp` Set to true when this represents a DateTime as opposed to Bignum This is needed so scale can be correctly interpreted since scale encodes totally different info in the two cases.
`static byte` `TU_DAY`
`static java.util.Hashtable<java.lang.Integer,java.lang.Integer>` `TU_Exp_Table`
`static byte` `TU_F1`
`static byte` `TU_F2`
`static byte` `TU_F3`
`static byte` `TU_F4`
`static byte` `TU_F5`
`static byte` `TU_FRAC`
`static byte` `TU_HOUR`
`static byte` `TU_MINUTE`
`static byte` `TU_MONTH`
`static byte` `TU_SECOND`
`static byte` `TU_YEAR`

### Constructor Summary

Constructors
Constructor and Description
`Decimal()` Null Decimal constructor Dont need to fill any fields since default fields are for a NULL decimal
`Decimal(java.math.BigDecimal n)` Constructs a Decimal given a java.lang.Bignum Unfortunately, Bignum does NOT provide access to its data members so we have to take the roundabout way of converting to string.
`Decimal(boolean sign, byte[] b, int blen, short inPrec)`
`Decimal(byte[] cp)` For debugging only Unlike the other constructors, this one expects bytes[0,1] to contain the length of the remaining bytes.
`Decimal(byte[] cp, int offset, int length, short prec)` Converts bytes as received from IFX server into a Decimal obj
`Decimal(byte[] cp, int offset, int length, short prec, boolean ts)` Converts bytes as received from IFX server into a Decimal obj but interpreted as a timestamp data
`Decimal(byte[] cp, short prec)` Converts bytes as received from IFX server into a Decimal obj
`Decimal(byte[] cp, short prec, boolean ts)` Converts bytes as received from IFX server into a Decimal obj but interpreted as a timestamp data
`Decimal(IfxDateTime dateTime)`
`Decimal(int yyyy, int mo, int dd, int hh, int mm, int ss, int fffff)`
`Decimal(short exp, short pos, short ndgts, byte[] d)` Constructor given basic fields as separate arg
`Decimal(java.lang.String num)` Creates Decimal from a string form of a decimal number of the form created by Bignum or by hand.
`Decimal(java.lang.String num, short inPrec)`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`static void` `bycopy(byte[] from, int fromStart, byte[] to, int toStart)` Copies bytes (or as many will fit) of first array into second starting with the fromStart pos.
`static byte[]` `byfill(byte[] b, byte f)` Fills given byte array with the given fill byte
`static void` `charcopy(char[] from, int fromStart, int fromEnd, char[] to, int toStart)` Copies all chars (or as many will fit) of first array into second starting with fromStart upto fromEnd (both incl) It is expected that to.length - toStart >= fromEnd - fromStart+1
`static char[]` `charfill(char[] b, int from, int to, char f)` Fills given char array (from -> to, both incl) with the given fill char
`protected void` `computePrecision()`
`protected void` `computeScale()`
`static Decimal` `dec_tToJava(byte[] b)`
`static byte[]` `decComplement(byte[] b, int start)` Takes 100s complement of the given byte array starting from the end, backwards upto the "start" byte Process: byte[n] = 100- byte[n] byte[0] ..
`java.lang.Double` `doubleValue()` Returns a Double equivalent of this.
`static byte[]` `encodeDateTimePrecision(byte[] b, byte s, byte e)` Encodes precision for Datetime for transmission to IFX See the macro TU_DTENCODE in the IFX file datetime.h
`static byte[]` `encodeIntervalPrecision(byte[] b, byte len, byte s, byte e)` Encodes precision for interval for transmission to IFX See the macro TU_IENCODE in the IFX file datetime.h
`protected int` `extractExponent(java.lang.String num, int start)` Given number string checks if it contains an exponent checking from start char.
`java.lang.Float` `floatValue()` Returns a Float equivalent of this.
`static byte[]` `fromBindToTuple(byte[] bindFormat, int newLength)`
`byte[]` `getPrecision()` Returns the precision as a 2-byte array
`protected void` `init(byte[] cp, int offset, int length, short prec)` Given a packed byte array of the form received from IFX servers creates Decimal.
`Interval` `intervalValue()`
`boolean` `isNull()`
`byte[]` `javaToDec_t()` Convert Decimal to byte array that's in Informix dec_t format as defined in decimal.h.
`byte[]` `javaToIfx()` Converts Decimal to byte array for sending out to IFX servers The format of the bytes is as follows: b[0], b[1] - length of bytes from b[2] on b[2] ..
`static Decimal` `numericToDecimal(java.math.BigDecimal n)` Converts given Bignum to Decimal
`java.math.BigDecimal` `numericValue()` Converts this to a Bignum with sufficient precision and scale equal to whatever is needed based on the value of this
`void` `setT()`
`void` `setTimestampQualifier(short qualifier)`
`char[]` `sigdigToChar()` Converts significant digits of this (base 100) to a char[] Shouldnt be called if there are no significant digits
`int` `sigdigToInt(int from, int to)` Converts specified digits of this (base 100) to an int
`java.lang.String` `stringValue()` Creates a string representing the decimal value in a form that can be used to create an equivalent Bignum If NULL returns an empty string TBD ??? Use scale to pad with extra zeros to right if needed
`java.sql.Timestamp` `timestampValue()` Interprets this as a DateTime that was encoded as a decimal by IFX and returns a java.sql.Timestamp.
`java.sql.Timestamp` `timestampValue(java.util.Calendar cal)`
`protected static char[]` `trimZerosFB(char[] ibuf, int start)` Given char array of digits and starting position returns a new array with leading and trailing zeros stripped out Leaves 0th element of returned array empty for sign
`static byte` `tuEnd(byte c)`
`static int` `tuFLen(short len)`
`static byte` `tuLen(short c)`
`static byte` `tuStart(byte c)`
`void` `unpackPrecision(short prec)` Interprets prec to extract: pdigs - precision, i.e., total digits pscale - scale, i.e., digits after decimal pt to retain Note: Interprets low byte depending on whether timestamp flg is set If timestamp, then uses low 4 bits of low byte as end time indicator (@see #TU_YEAR etc) and computes scale from that.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### digChar

    public static final java.lang.String digChar

See Also:
  Constant Field Values

#### DEC_T_SIZE

    public static final int DEC_T_SIZE

number of bytes in a C dec_t struct sizeof(short) * 3 + DECSIZE = 22

See Also:
  Constant Field Values

#### DECSIZE

    public static final int DECSIZE

number of bytes allocated for IFX DECIMAL

See Also:
  Constant Field Values

#### DECPOSNULL

    public static final short DECPOSNULL

indicates NULL decimal number

See Also:
  Constant Field Values

#### DECPOSPOSITIVE

    public static final short DECPOSPOSITIVE

indicates POSITIVE decimal number

See Also:
  Constant Field Values

#### DECPOSNEGATIVE

    public static final short DECPOSNEGATIVE

indicates NEGATIVE decimal number

See Also:
  Constant Field Values

#### dec_exp

    protected short dec_exp

Exponent in base 100

#### dec_pos

    protected short dec_pos

Sign. 1 (pos), 0 (neg) -1 (null)

#### dec_ndgts

    protected short dec_ndgts

Base 100 digits

#### dec_dgts

    protected byte[] dec_dgts

Array of base 100 digits (2 per byte)

#### lenOfFirstField

    protected int lenOfFirstField

#### precStored

    protected short precStored

#### pdigs

    protected byte pdigs

Total significant digits. dec_ndgts = If this is odd then (pdigs+1)/2 else pdigs/2

See Also:
  `dec_ndgts`

#### pscale

    protected byte pscale

Scale, i.e., digits after decimal point

#### timestamp

    protected boolean timestamp

Set to true when this represents a DateTime as opposed to Bignum This is needed so scale can be correctly interpreted since scale encodes totally different info in the two cases.

#### startCode

    protected byte startCode

startCode is the start DateTime code as received from IFX Only relevent when timestamp = true

#### endCode

    protected byte endCode

endCode is the end DateTime code as received from IFX Only relevent when timestamp = true

#### TU_YEAR

    public static final byte TU_YEAR

See Also:
  Constant Field Values

#### TU_MONTH

    public static final byte TU_MONTH

See Also:
  Constant Field Values

#### TU_DAY

    public static final byte TU_DAY

See Also:
  Constant Field Values

#### TU_HOUR

    public static final byte TU_HOUR

See Also:
  Constant Field Values

#### TU_MINUTE

    public static final byte TU_MINUTE

See Also:
  Constant Field Values

#### TU_SECOND

    public static final byte TU_SECOND

See Also:
  Constant Field Values

#### TU_FRAC

    public static final byte TU_FRAC

See Also:
  Constant Field Values

#### TU_F1

    public static final byte TU_F1

See Also:
  Constant Field Values

#### TU_F2

    public static final byte TU_F2

See Also:
  Constant Field Values

#### TU_F3

    public static final byte TU_F3

See Also:
  Constant Field Values

#### TU_F4

    public static final byte TU_F4

See Also:
  Constant Field Values

#### TU_F5

    public static final byte TU_F5

See Also:
  Constant Field Values

#### TU_Exp_Table

    public static java.util.Hashtable<java.lang.Integer,java.lang.Integer> TU_Exp_Table

### Constructor Detail

#### Decimal

    public Decimal()

Null Decimal constructor Dont need to fill any fields since default fields are for a NULL decimal

#### Decimal

    public Decimal(short exp,
                   short pos,
                   short ndgts,
                   byte[] d)

Constructor given basic fields as separate arg

Parameters:
  `exp` - base 100 exponent
  `pos` - sign (1 pos, 0, neg, -1 null)
  `ndgts` - base 100 digits
  `d` - array of digits packed 2 per byte

#### Decimal

    public Decimal(byte[] cp)

For debugging only Unlike the other constructors, this one expects bytes[0,1] to contain the length of the remaining bytes. Use this for debugging only to feed back the byte array produced by javaToIfx(). Note: As of 5/10/96, precision is returned via a separate method But since it is not available pass a 0 by default

#### Decimal

    public Decimal(byte[] cp,
                   short prec)

Converts bytes as received from IFX server into a Decimal obj

Parameters:
  `cp` - should contain only the data bytes
  `prec` - precision

#### Decimal

    public Decimal(byte[] cp,
                   int offset,
                   int length,
                   short prec)

Converts bytes as received from IFX server into a Decimal obj

Parameters:
  `cp` - byte array containing a decimal in network format
  `offset` - offset into array where decimal data begins
  `length` - length of decimal data in byte array
  `prec` - precision

#### Decimal

    public Decimal(byte[] cp,
                   short prec,
                   boolean ts)

Converts bytes as received from IFX server into a Decimal obj but interpreted as a timestamp data

Parameters:
  `cp` - should contain only the data bytes
  `prec` - precision
  `ts` - if true then interped as timestamp. default is false

#### Decimal

    public Decimal(byte[] cp,
                   int offset,
                   int length,
                   short prec,
                   boolean ts)

Converts bytes as received from IFX server into a Decimal obj but interpreted as a timestamp data

Parameters:
  `cp` - should contain only the data bytes
  `offset` - offset into cp byte array
  `length` - length of data
  `prec` - precision
  `ts` - if true then interped as timestamp. default is false

#### Decimal

    public Decimal(java.lang.String num)

Creates Decimal from a string form of a decimal number of the form created by Bignum or by hand.

Parameters:
  `string` - the string from which Decimal is created Format of input string: Optional sign in first place. Optional decimal pt anywhere Optional exponent of the form "e+-nnn" at end. However, if exponent is given then a decimal pt must precede it somewhere in the number. Thus, "123e-034" is invalid. This format is based on the one apparently used by java floats. No other chars allowed. Dont add spaces either!!

#### Decimal

    public Decimal(java.lang.String num,
                   short inPrec)

#### Decimal

    public Decimal(java.math.BigDecimal n)

Constructs a Decimal given a java.lang.Bignum Unfortunately, Bignum does NOT provide access to its data members so we have to take the roundabout way of converting to string. Sigh!!

Parameters:
  `n` - Bignum from which equivalent Decimal is created

#### Decimal

    public Decimal(boolean sign,
                   byte[] b,
                   int blen,
                   short inPrec)

#### Decimal

    public Decimal(int yyyy,
                   int mo,
                   int dd,
                   int hh,
                   int mm,
                   int ss,
                   int fffff)

#### Decimal

    public Decimal(IfxDateTime dateTime)

### Method Detail

#### setTimestampQualifier

    public final void setTimestampQualifier(short qualifier)

#### setT

    public final void setT()

#### unpackPrecision

    public final void unpackPrecision(short prec)

Interprets prec to extract: pdigs - precision, i.e., total digits pscale - scale, i.e., digits after decimal pt to retain Note: Interprets low byte depending on whether timestamp flg is set If timestamp, then uses low 4 bits of low byte as end time indicator (@see #TU_YEAR etc) and computes scale from that. Else treats low byte as number of digits after dec pt

#### computePrecision

    protected void computePrecision()

#### computeScale

    protected void computeScale()

#### getPrecision

    public byte[] getPrecision()

Returns the precision as a 2-byte array

#### init

    protected void init(byte[] cp,
                        int offset,
                        int length,
                        short prec)

Given a packed byte array of the form received from IFX servers creates Decimal. The precision and length bytes should be stripped out before this is called. Note: Interprets the byte array in a very special form so dont use this unless the byte array is really coming from IFX servers or strictly equivalent code.

Parameters:
  `cp` - byte[0] - contains the exponent in an encoded form rest of the bytes - packed digits in the number (base 100)
  `prec` - encoded information about the precision of the decimal with b[0] - precision and b[1] - scale if prec =0 then uses the precision inherent in the number

#### extractExponent

    protected int extractExponent(java.lang.String num,
                                  int start)

Given number string checks if it contains an exponent checking from start char. Exponent if given must be of the form: e+-nnn. Returns a number equal to the exponent Returns 0 if no exponent found.

Parameters:
  `num` - number as a string which may have an exponent
  `start` - position where to start looking in string

#### byfill

    public static byte[] byfill(byte[] b,
                                byte f)

Fills given byte array with the given fill byte

#### charfill

    public static char[] charfill(char[] b,
                                  int from,
                                  int to,
                                  char f)

Fills given char array (from -> to, both incl) with the given fill char

Parameters:
  `b` - array to fill
  `from` - index of first position to fill
  `to` - index of last position to fill
  `f` - fill character

#### bycopy

    public static void bycopy(byte[] from,
                              int fromStart,
                              byte[] to,
                              int toStart)

Copies bytes (or as many will fit) of first array into second starting with the fromStart pos. Note that the second array receives starting from its toStart byte

Parameters:
  `from` - source array
  `fromStart` - index of first byte copied
  `to` - target array
  `toStart` - index of first recipient location in to

#### charcopy

    public static void charcopy(char[] from,
                                int fromStart,
                                int fromEnd,
                                char[] to,
                                int toStart)

Copies all chars (or as many will fit) of first array into second starting with fromStart upto fromEnd (both incl) It is expected that to.length - toStart >= fromEnd - fromStart+1

Parameters:
  `from` - source array
  `fromStart` - index of first char copied
  `fromEnd` - index of last char copied
  `to` - target array
  `toStart` - index of first recipient location in to

#### decComplement

    public static byte[] decComplement(byte[] b,
                                       int start)

Takes 100s complement of the given byte array starting from the end, backwards upto the "start" byte Process: byte[n] = 100- byte[n] byte[0] .. byte[n-1] = 99 - byte[i]

Parameters:
  `b` - array of bytes packed with base 100 digits
  `start` - index of first byte to complement

#### javaToIfx

    public byte[] javaToIfx()

Converts Decimal to byte array for sending out to IFX servers The format of the bytes is as follows: b[0], b[1] - length of bytes from b[2] on b[2] .. packed format as defined in decimal.h Note: While the precision is also transmitted to IFX, it is computed separately and written to the "output" before these bytes.

#### javaToDec_t

    public byte[] javaToDec_t()

Convert Decimal to byte array that's in Informix dec_t format as defined in decimal.h.

#### dec_tToJava

    public static Decimal dec_tToJava(byte[] b)

#### sigdigToChar

    public char[] sigdigToChar()

Converts significant digits of this (base 100) to a char[] Shouldnt be called if there are no significant digits

#### sigdigToInt

    public int sigdigToInt(int from,
                           int to)

Converts specified digits of this (base 100) to an int

Parameters:
  `from` - first base 100 digit
  `to` - last base 100 digit

#### numericValue

    public java.math.BigDecimal numericValue()

Converts this to a Bignum with sufficient precision and scale equal to whatever is needed based on the value of this

#### isNull

    public boolean isNull()

#### doubleValue

    public final java.lang.Double doubleValue()

Returns a Double equivalent of this. Note: A loss of precision may result if this was NOT originally created from a double

#### floatValue

    public final java.lang.Float floatValue()

Returns a Float equivalent of this. Note: A loss of precision may result if this was NOT originally created from a Float

#### stringValue

    public java.lang.String stringValue()

Creates a string representing the decimal value in a form that can be used to create an equivalent Bignum If NULL returns an empty string TBD ??? Use scale to pad with extra zeros to right if needed

#### trimZerosFB

    protected static char[] trimZerosFB(char[] ibuf,
                                        int start)

Given char array of digits and starting position returns a new array with leading and trailing zeros stripped out Leaves 0th element of returned array empty for sign

#### fromBindToTuple

    public static byte[] fromBindToTuple(byte[] bindFormat,
                                         int newLength)

#### tuEnd

    public static byte tuEnd(byte c)

#### tuStart

    public static byte tuStart(byte c)

#### tuLen

    public static byte tuLen(short c)

#### encodeDateTimePrecision

    public static byte[] encodeDateTimePrecision(byte[] b,
                                                 byte s,
                                                 byte e)

Encodes precision for Datetime for transmission to IFX See the macro TU_DTENCODE in the IFX file datetime.h

Parameters:
  `b` - 2 byte array where precision is created
  `s` - byte containing code for start time unit
  `e` - byte containing code for end time unit
See Also:
  `TU_YEAR`, `TU_MONTH`, `TU_F5`

#### encodeIntervalPrecision

    public static byte[] encodeIntervalPrecision(byte[] b,
                                                 byte len,
                                                 byte s,
                                                 byte e)

Encodes precision for interval for transmission to IFX See the macro TU_IENCODE in the IFX file datetime.h

Parameters:
  `b` - 2 byte array where precision is created
  `s` - byte containing code for start time unit
  `e` - byte containing code for end time unit
See Also:
  `TU_YEAR`, `TU_MONTH`, `TU_F5`

#### tuFLen

    public static int tuFLen(short len)

#### timestampValue

    public java.sql.Timestamp timestampValue()

Interprets this as a DateTime that was encoded as a decimal by IFX and returns a java.sql.Timestamp. Interprets the pscale as follows: high 4 bits: Start Time unit (0-15) low 4 bits: End Time Unit (0-15) These time units are strictly as defined in IFX datetime.h They are also defined in this file as TU_xxx Note: IFX DateTime can specify a date in a subset of the full range [YR .. NANOSEC] allowed by Java Timestamp. This creates two problems a. What to do with partial IFX DateTimes when converting to Java Timestamp? Backfill current date? Set unspecified fields to 0? Solution: Unspecified higher precision fields are backfilled from current time. Lower precision ones are set to 0. Thus if precision in IFX was MONTH .. MIN, then year is taken from current but SEC and NANO set to 0 b. How to send Java Timestamp back to IFX when the IFX is only expecting a partial DateTime (based on the precision there). The problem here is how best to preserve the precision info that came from IFX and use it on the way back. Solution: Send as in the java.Timestamp with fractional part truncated to 5 digits

#### timestampValue

    public java.sql.Timestamp timestampValue(java.util.Calendar cal)

#### intervalValue

    public Interval intervalValue()

#### numericToDecimal

    public static Decimal numericToDecimal(java.math.BigDecimal n)

Converts given Bignum to Decimal