]> git.ktnx.net Git - mobile-ledger-staging.git/commitdiff
add support for floatingPoint member of the quantity objects
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 27 Sep 2020 17:09:14 +0000 (20:09 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 27 Sep 2020 17:09:14 +0000 (20:09 +0300)
introduced in hledger-web to avoid problems with large mantissas and
such when some prices are drived from transactions that result in
periodic decimals

app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedQuantity.java
app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedQuantity.java

index 5c27f6fbab8fe10e05075da1ed5aad1f062f6d01..2588054ffb9419d70191c47330ac153b062973dc 100644 (file)
@@ -21,8 +21,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 
 @JsonIgnoreProperties(ignoreUnknown = true)
 public class ParsedQuantity {
-    private long decimalMantissa;
-    private int decimalPlaces;
+    protected long decimalMantissa;
+    protected int decimalPlaces;
     public ParsedQuantity() {
     }
     public ParsedQuantity(String input) {
index f40968307301df8aa41ee7652bc4003d055a2aa7..7ce1d12d3816efc5f1bc0a8b9906a7e1748105ee 100644 (file)
@@ -20,39 +20,21 @@ package net.ktnx.mobileledger.json.v1_15;
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 
 @JsonIgnoreProperties(ignoreUnknown = true)
-public class ParsedQuantity {
-    private long decimalMantissa;
-    private int decimalPlaces;
-    public ParsedQuantity() {
-    }
-    public ParsedQuantity(String input) {
-        parseString(input);
-    }
-    public long getDecimalMantissa() {
-        return decimalMantissa;
-    }
-    public void setDecimalMantissa(long decimalMantissa) {
-        this.decimalMantissa = decimalMantissa;
-    }
-    public int getDecimalPlaces() {
-        return decimalPlaces;
-    }
-    public void setDecimalPlaces(int decimalPlaces) {
-        this.decimalPlaces = decimalPlaces;
+public class ParsedQuantity extends net.ktnx.mobileledger.json.v1_14.ParsedQuantity {
+    private float floatingPoint;
+    private boolean floatingPointSet = false;
+    public float getFloatingPoint() {
+        if (!floatingPointSet)
+            throw new IllegalStateException("floatingPoint is not set");
+        return floatingPoint;
+    }
+    public void setFloatingPoint(float floatingPoint) {
+        this.floatingPoint = floatingPoint;
+        floatingPointSet = true;
     }
     public float asFloat() {
+        if (floatingPointSet)
+            return floatingPoint;
         return (float) (decimalMantissa * Math.pow(10, -decimalPlaces));
     }
-    public void parseString(String input) {
-        int pointPos = input.indexOf('.');
-        if (pointPos >= 0) {
-            String integral = input.replace(".", "");
-            decimalMantissa = Long.parseLong(integral);
-            decimalPlaces = input.length() - pointPos - 1;
-        }
-        else {
-            decimalMantissa = Long.parseLong(input);
-            decimalPlaces = 0;
-        }
-    }
 }