-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use BigDecimal instead of Double to avoid loss of precision
- Loading branch information
Showing
2 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/org/codehaus/jettison/json/JSONTokenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.codehaus.jettison.json; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class JSONTokenerTest extends TestCase { | ||
|
||
public void testDoublePrecision() throws Exception { | ||
JSONTokener doubleTokener = new JSONTokener("9999999999999.9999"); | ||
Object nextValue = doubleTokener.nextValue(); | ||
assertEquals(Double.class, nextValue.getClass()); | ||
assertEquals(Double.valueOf("1.0E13"), nextValue); | ||
} | ||
|
||
public void testBigDecimalPrecision() throws Exception { | ||
JSONTokener bigDecimalTokener = new JSONTokener("9999999999999.9999") { | ||
{ | ||
this.useBigDecimal = true; | ||
} | ||
}; | ||
Object nextValue = bigDecimalTokener.nextValue(); | ||
assertEquals(BigDecimal.class, nextValue.getClass()); | ||
assertEquals(new BigDecimal("9999999999999.9999"), nextValue); | ||
} | ||
|
||
} |