1919
2020import android .support .annotation .NonNull ;
2121
22- import org .gnucash .android .model .Money ;
23- import org .gnucash .android .model .Split ;
24- import org .gnucash .android .model .TransactionType ;
22+ import org .gnucash .android .ui .transaction .TransactionFormFragment ;
2523
2624import java .math .BigDecimal ;
2725import java .math .BigInteger ;
@@ -162,41 +160,8 @@ public static long parseDate(String dateString) throws ParseException {
162160 }
163161
164162 /**
165- * Parses the amounts in template transaction splits.
166- * <p>GnuCash desktop formats the amounts based on the locale of the computer. That poses a problem here as the input can vary per user.<br/>
167- * The solution is to parse the string irrespective of comma or thousands separators as follows:
168- * <ol>
169- * <li>Find the last non-numeric character and split the string at that point</li>
170- * <li>If the length of the 2nd part is >= 2, then it is a thousands separator, else it is decimal</li>
171- * <li>Append the two parts again together accordingly</li>
172- * <li>If no non-numeric character was found, then just return a new {@link BigDecimal}</li>
173- * </ol>
174- * </p>
175- * @param amountString String value of the amount.
176- * @return BigDecimal representation of the amount
177- * @see #formatTemplateSplitAmount(BigDecimal)
178- */
179- public static BigDecimal parseTemplateSplitAmount (@ NonNull String amountString ){
180- Pattern pattern = Pattern .compile (".*\\ D" );
181- Matcher matcher = pattern .matcher (amountString );
182- if (matcher .find ()){
183- int index = matcher .end ();
184- String wholeNum = amountString .substring (0 , index ).replaceAll ("\\ D" , "" );
185- String decimal = amountString .substring (index );
186- String parsedAmountString ;
187- if (decimal .length () > 2 ){ //then it is just another thousands separator, just add it back
188- parsedAmountString = wholeNum + decimal ;
189- } else { //add it as a decimal
190- parsedAmountString = wholeNum + "." + decimal ;
191- }
192- return new BigDecimal (parsedAmountString );
193- } else {//an amount string with no commas or periods
194- return new BigDecimal (amountString );
195- }
196- }
197-
198- /**
199- * Parses amount strings from GnuCash XML into {@link java.math.BigDecimal}s
163+ * Parses amount strings from GnuCash XML into {@link java.math.BigDecimal}s.
164+ * The amounts are formatted as 12345/4100
200165 * @param amountString String containing the amount
201166 * @return BigDecimal with numerical value
202167 * @throws ParseException if the amount could not be parsed
@@ -207,9 +172,11 @@ public static BigDecimal parseSplitAmount(String amountString) throws ParseExcep
207172 {
208173 throw new ParseException ("Cannot parse money string : " + amountString , 0 );
209174 }
210- BigInteger numerator = new BigInteger (amountString .substring (0 , pos ));
211- int scale = amountString .length () - pos - 2 ;
212- return new BigDecimal (numerator , scale );
175+
176+ int scale = amountString .length () - pos - 2 ; //do this before, because we could modify the string
177+ String numerator = TransactionFormFragment .stripCurrencyFormatting (amountString .substring (0 , pos ));
178+ BigInteger numeratorInt = new BigInteger (numerator );
179+ return new BigDecimal (numeratorInt , scale );
213180 }
214181
215182 /**
@@ -224,7 +191,8 @@ public static String formatSplitAmount(BigDecimal amount, Currency trxCurrency){
224191 BigDecimal denom = new BigDecimal (denomInt );
225192 String denomString = Integer .toString (denomInt );
226193
227- return amount .multiply (denom ).stripTrailingZeros ().toPlainString () + "/" + denomString ;
194+ String numerator = TransactionFormFragment .stripCurrencyFormatting (amount .multiply (denom ).stripTrailingZeros ().toPlainString ());
195+ return numerator + "/" + denomString ;
228196 }
229197
230198 /**
0 commit comments