public class MyDecimal extends Object implements Serializable
Constructor and Description |
---|
MyDecimal() |
MyDecimal(int digitsInt,
int digitsFrac,
boolean negative,
int[] wordBuf) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears this instance.
|
int |
frac()
Returns fraction digits that counts how many digits after ".".
|
int |
fromBin(int precision,
int frac,
int[] bin)
Parses a decimal from binary string for given precision and frac.
|
void |
fromDecimal(double value)
Parses a decimal value from a string
|
void |
fromString(String s)
parser a decimal value from a string.
|
int |
precision() |
static int |
readWord(int[] b,
int size,
int start)
Reads a word from a array at given size.
|
BigDecimal |
toBigDecimal() |
int[] |
toBin(int precision,
int frac)
ToBin converts decimal to its binary fixed-length representation two representations of the
same length can be compared with memcmp with the correct -1/0/+1 result
|
long |
toLong() |
String |
toString() |
public MyDecimal()
public MyDecimal(int digitsInt, int digitsFrac, boolean negative, int[] wordBuf)
public static int readWord(int[] b, int size, int start)
b
- b is source data of unsigned byte as int[]size
- is word size which can be used in switch statement.start
- start indicates the where start to read.public int precision()
public int frac()
public void fromDecimal(double value)
value
- an double valuepublic int fromBin(int precision, int frac, int[] bin)
precision
- precision specifies total digits that this decimal will be..frac
- frac specifies how many fraction digitsbin
- bin is binary string which represents a decimal value.public void fromString(String s)
s
- s is a decimal in string form.public int[] toBin(int precision, int frac)
PARAMS precision/frac - if precision is 0, internal value of the decimal will be used, then the encoded value is not memory comparable.
NOTE the buffer is assumed to be of the size decimalBinSize(precision, frac)
RETURN VALUE bin - binary value errCode - eDecOK/eDecTruncate/eDecOverflow
DESCRIPTION for storage decimal numbers are converted to the "binary" format.
This format has the following properties: 1. length of the binary representation depends on the {precision, frac} as provided by the caller and NOT on the digitsInt/digitsFrac of the decimal to convert. 2. binary representations of the same {precision, frac} can be compared with memcmp - with the same result as DecimalCompare() of the original decimals (not taking into account possible precision loss during conversion).
This binary format is as follows: 1. First the number is converted to have a requested precision and frac. 2. Every full digitsPerWord digits of digitsInt part are stored in 4 bytes as is 3. The first digitsInt % digitsPerWord digits are stored in the reduced number of bytes (enough bytes to store this number of digits - see dig2bytes) 4. same for frac - full word are stored as is, the last frac % digitsPerWord digits - in the reduced number of bytes. 5. If the number is negative - every byte is inverted. 5. The very first bit of the resulting byte array is inverted (because memcmp compares unsigned bytes, see property 2 above)
Example:
1234567890.1234
internally is represented as 3 words
1 234567890 123400000
(assuming we want a binary representation with precision=14, frac=4) in hex it's
00-00-00-01 0D-FB-38-D2 07-5A-EF-40
now, middle word is full - it stores 9 decimal digits. It goes into binary representation as is:
........... 0D-FB-38-D2 ............
First word has only one decimal digit. We can store one digit in one byte, no need to waste four:
01 0D-FB-38-D2 ............
now, last word. It's 123400000. We can store 1234 in two bytes:
01 0D-FB-38-D2 04-D2
So, we've packed 12 bytes number in 7 bytes. And now we invert the highest bit to get the final result:
81 0D FB 38 D2 04 D2
And for -1234567890.1234 it would be
7E F2 04 C7 2D FB 2D return a int array which represents a decimal value.
precision
- precision for decimal value.frac
- fraction for decimal value.public void clear()
public long toLong()
public BigDecimal toBigDecimal()
Copyright © 2024 PingCAP. All rights reserved.