File tree Expand file tree Collapse file tree
main/java/org/gnucash/android/model
test/java/org/gnucash/android/test/unit/model Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55
66import java .math .BigDecimal ;
7+ import java .math .MathContext ;
78import java .sql .Timestamp ;
89import java .text .DecimalFormat ;
910import java .text .NumberFormat ;
@@ -156,6 +157,6 @@ public String toString() {
156157 BigDecimal denominator = new BigDecimal (mValueDenom );
157158 DecimalFormat formatter = (DecimalFormat ) NumberFormat .getNumberInstance ();
158159 formatter .setMaximumFractionDigits (6 );
159- return formatter .format (numerator .divide (denominator ));
160+ return formatter .format (numerator .divide (denominator , MathContext . DECIMAL32 ));
160161 }
161162}
Original file line number Diff line number Diff line change 2323import java .util .Locale ;
2424
2525import static org .assertj .core .api .Assertions .assertThat ;
26+ import static org .junit .Assert .fail ;
2627
2728
2829public class PriceTest {
@@ -52,6 +53,26 @@ public void toString_shouldUseDefaultLocale() {
5253 assertThat (price .toString ()).isEqualTo ("1,234" );
5354 }
5455
56+ /**
57+ * BigDecimal throws an ArithmeticException if it can't represent exactly
58+ * a result. This can happen with divisions like 1/3 if no precision and
59+ * round mode is specified with a MathContext.
60+ */
61+ @ Test
62+ public void toString_shouldNotFailForInfinitelyLongDecimalExpansion () {
63+ long numerator = 1 ;
64+ long denominator = 3 ;
65+ Price price = new Price ();
66+
67+ price .setValueNum (numerator );
68+ price .setValueDenom (denominator );
69+ try {
70+ price .toString ();
71+ } catch (ArithmeticException e ) {
72+ fail ("The numerator/denominator division in Price.toString() should not fail." );
73+ }
74+ }
75+
5576 @ Test
5677 public void getNumerator_shouldReduceAutomatically () {
5778 long numerator = 1 ;
You can’t perform that action at this time.
0 commit comments