@@ -40,6 +40,73 @@ This library explicitly does not include mathematical operations on decimal.
4040As an alternative, consider using ` BigDecimal ` with ` MathContext.DECIMAL128 ` ,
4141` MathContext.DECIMAL64 ` , or ` MathContext.DECIMAL32 ` .
4242
43+ Usage
44+ -----
45+
46+ Decoding a 4-byte Decimal32 to a ` java.math.BigDecimal ` :
47+
48+ ``` java
49+ byte [] bytes = {(byte ) 0xc7 , (byte ) 0xf4 , (byte ) 0xd2 , (byte ) 0xe7 };
50+ Decimal32 decimal32 = Decimal32 . parseBytes(bytes);
51+ BigDecimal bigDecimal = decimal32. toBigDecimal();
52+ assertEquals(new BigDecimal (" -1.234567E+96" ), bigDecimal);
53+ ```
54+
55+ The method ` toBigDecimal ` throws ` DecimalInconvertibleException ` if the decimal
56+ value is an infinity or NaN value. The actual type and sign can be obtained from
57+ the exception.
58+
59+ Encoding a ` java.math.BigDecimal ` to Decimal32 byte array:
60+
61+ ``` java
62+ BigDecimal bigDecimal = new BigDecimal (" -7.50E-7" );
63+ Decimal32 decimal32 = Decimal32 . valueOf(bigDecimal);
64+ byte [] bytes = decimal32. toBytes();
65+ assertArrayEquals(new byte [] {(byte ) 0xa1 , (byte ) 0xc0 , 0x03 , (byte ) 0xd0 }, bytes);
66+ ```
67+
68+ This will apply rounding if ` bigDecimal ` value doesn't fit a Decimal32, and
69+ overflow will 'round' to infinity.
70+
71+ If overflow to infinity is unwanted, then use:
72+
73+ ``` java
74+ BigDecimal bigDecimal = new BigDecimal (" -7.50E-7" );
75+ Decimal32 decimal32 = Decimal32 . valueOf(bigDecimal, OverflowHandling . THROW_EXCEPTION );
76+ byte [] bytes = decimal32. toBytes();
77+ assertArrayEquals(new byte [] {(byte ) 0xa1 , (byte ) 0xc0 , 0x03 , (byte ) 0xd0 }, bytes);
78+ ```
79+
80+ Conversion works the same for ` Decimal64 ` and ` Decimal128 ` .
81+
82+ The ` valueOf ` methods exists for:
83+
84+ - ` BigDecimal `
85+ - ` BigInteger `
86+ - In addition there is ` valueOfExact(BigInteger) ` which throws
87+ ` DecimalOverflowException ` if the ` BigInteger ` needs to be rounded to fit the
88+ target decimal type.
89+ - ` String `
90+ - ` double `
91+ - ` Decimal ` (parent class of ` Decimal32 ` , ` Decimal64 ` and ` Decimal128 ` ) to allow
92+ conversion between decimal types
93+
94+ The ` valueOf ` methods will round values to fit the target decimal type, and -
95+ depending on the specified overflow handling - will either return +/- infinity
96+ or throw an exception on overflow.
97+
98+ Conversion to a type is provided by:
99+
100+ - ` toBytes() `
101+ - ` toBigDecimal() ` - will throw ` DecimalInconvertibleException ` if the value is
102+ an infinity or NaN value
103+ - ` toString() `
104+ - ` doubleValue() `
105+ - ` toDecimal(Class) ` and ` toDecimal(Class, OverflowHandling) `
106+
107+ To obtain a ` BigInteger ` , use ` toBigDecimal().toBigInteger() ` but be aware that
108+ large values (especially of ` Decimal128 ` ) can result in significant memory use.
109+
43110Background
44111----------
45112
@@ -69,6 +136,6 @@ References
69136 - [ Decimal Arithmetic Encodings] ( http://speleotrove.com/decimal/decbits.html )
70137 - [ A Summary of Densely Packed Decimal encoding] ( http://speleotrove.com/decimal/DPDecimal.html )
71138 - [ The decNumber Library] ( http://speleotrove.com/decimal/decnumber.html )
72- - [ Firebird 4 alpha 1 release notes] ( http://web.firebirdsql.org/downloads/prerelease/v40alpha1 /Firebird-4.0.0_Alpha1 -ReleaseNotes.pdf )
139+ - [ Firebird 4 beta 1 release notes] ( http://web.firebirdsql.org/downloads/prerelease/v40beta1 /Firebird-4.0.0_Beta1 -ReleaseNotes.pdf )
73140
74141
0 commit comments