Skip to content

Commit a4ad119

Browse files
committed
Added some basic usage example
1 parent b99c9b1 commit a4ad119

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,73 @@ This library explicitly does not include mathematical operations on decimal.
4040
As 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+
43110
Background
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

devdoc/deploy.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ In addition, you need to set the following credentials
2727
./gradlew addCredentials --key signing.password --value <your secret key password> -PcredentialsPassphrase=<credentials password>
2828
./gradlew addCredentials --key ossrhPassword --value <your sonatyp OSSRH password> -PcredentialsPassphrase=<credentials password>
2929
```
30+
31+
See https://github.com/etiennestuder/gradle-credentials-plugin for details on
32+
credentials.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2019 Firebird development team and individual contributors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.firebirdsql.decimal;
24+
25+
import org.junit.Test;
26+
27+
import java.math.BigDecimal;
28+
29+
import static org.junit.Assert.assertArrayEquals;
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* Tests for the examples in README.md.
34+
*
35+
* @author <a href="mailto:mark@lawinegevaar.nl">Mark Rotteveel</a>
36+
*/
37+
public class ExamplesTest {
38+
39+
@Test
40+
public void decimal32ParseBytes() {
41+
byte[] bytes = {(byte) 0xc7, (byte) 0xf4, (byte) 0xd2, (byte) 0xe7};
42+
Decimal32 decimal32 = Decimal32.parseBytes(bytes);
43+
BigDecimal bigDecimal = decimal32.toBigDecimal();
44+
assertEquals(new BigDecimal("-1.234567E+96"), bigDecimal);
45+
}
46+
47+
@Test
48+
public void decimal32ToBytes() {
49+
BigDecimal bigDecimal = new BigDecimal("-7.50E-7");
50+
Decimal32 decimal32 = Decimal32.valueOf(bigDecimal);
51+
byte[] bytes = decimal32.toBytes();
52+
assertArrayEquals(new byte[] {(byte) 0xa1, (byte) 0xc0, 0x03, (byte) 0xd0}, bytes);
53+
}
54+
55+
@Test
56+
public void decimal32ToBytes_overflowThrowException() {
57+
BigDecimal bigDecimal = new BigDecimal("-7.50E-7");
58+
Decimal32 decimal32 = Decimal32.valueOf(bigDecimal, OverflowHandling.THROW_EXCEPTION);
59+
byte[] bytes = decimal32.toBytes();
60+
assertArrayEquals(new byte[] {(byte) 0xa1, (byte) 0xc0, 0x03, (byte) 0xd0}, bytes);
61+
}
62+
}

0 commit comments

Comments
 (0)