Skip to content

Commit

Permalink
Merge pull request #1936 from ManfredKarrer/fix-formatting-issue
Browse files Browse the repository at this point in the history
Convert long dash to short dash for minus values
  • Loading branch information
ManfredKarrer committed Nov 17, 2018
2 parents 136c268 + 5f766b4 commit 108ce1d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
8 changes: 5 additions & 3 deletions core/src/main/java/bisq/core/monetary/Altcoin.java
Expand Up @@ -17,6 +17,8 @@

package bisq.core.monetary;

import bisq.core.util.BSFormatter;

import org.bitcoinj.core.Monetary;
import org.bitcoinj.utils.MonetaryFormat;

Expand Down Expand Up @@ -86,10 +88,10 @@ public String getCurrencyCode() {
*
* @throws IllegalArgumentException if you try to specify fractional satoshis, or a value out of range.
*/
public static Altcoin parseAltcoin(final String currencyCode, String inputValue) {
inputValue = inputValue.replace(",", ".");
public static Altcoin parseAltcoin(final String currencyCode, String input) {
String cleaned = BSFormatter.convertCharsForNumber(input);
try {
long val = new BigDecimal(inputValue).movePointRight(SMALLEST_UNIT_EXPONENT)
long val = new BigDecimal(cleaned).movePointRight(SMALLEST_UNIT_EXPONENT)
.toBigIntegerExact().longValue();
return Altcoin.valueOf(currencyCode, val);
} catch (ArithmeticException e) {
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/bisq/core/monetary/Price.java
Expand Up @@ -18,6 +18,7 @@
package bisq.core.monetary;

import bisq.core.locale.CurrencyUtil;
import bisq.core.util.BSFormatter;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Monetary;
Expand All @@ -31,12 +32,11 @@

/**
* Bitcoin price value with variable precision.
*
* <p>
* <br/>
* We wrap an object implementing the {@link Monetary} interface from bitcoinj. We respect the
* number of decimal digits of precision specified in the {@code smallestUnitExponent()}, defined in
* those classes, like {@link Fiat} or {@link Altcoin}.
*
*/
public class Price extends MonetaryWrapper implements Comparable<Price> {
private static final Logger log = LoggerFactory.getLogger(Price.class);
Expand All @@ -54,11 +54,11 @@ public Price(Monetary monetary) {
* Parse the Bitcoin {@code Price} given a {@code currencyCode} and {@code inputValue}.
*
* @param currencyCode The currency code to parse, e.g "USD" or "LTC".
* @param value The value to parse as a String, e.g "2.54" or "-0.0001".
* @return The parsed Price.
* @param input The input value to parse as a String, e.g "2.54" or "-0.0001".
* @return The parsed Price.
*/
public static Price parse(String currencyCode, String value) {
final String cleaned = value.replace(",", ".");
public static Price parse(String currencyCode, String input) {
String cleaned = BSFormatter.convertCharsForNumber(input);
if (CurrencyUtil.isFiatCurrency(currencyCode))
return new Price(Fiat.parseFiat(currencyCode, cleaned));
else
Expand All @@ -70,7 +70,7 @@ public static Price parse(String currencyCode, String value) {
*
* @param currencyCode The currency code to parse, e.g "USD" or "LTC".
* @param value The value to parse.
* @return The parsed Price.
* @return The parsed Price.
*/
public static Price valueOf(String currencyCode, long value) {
if (CurrencyUtil.isFiatCurrency(currencyCode)) {
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/bisq/core/monetary/Volume.java
Expand Up @@ -18,6 +18,7 @@
package bisq.core.monetary;

import bisq.core.locale.CurrencyUtil;
import bisq.core.util.BSFormatter;

import org.bitcoinj.core.Monetary;
import org.bitcoinj.utils.Fiat;
Expand All @@ -34,8 +35,8 @@ public Volume(Monetary monetary) {
super(monetary);
}

public static Volume parse(String inputValue, String currencyCode) {
final String cleaned = inputValue.replace(",", ".");
public static Volume parse(String input, String currencyCode) {
String cleaned = BSFormatter.convertCharsForNumber(input);
if (CurrencyUtil.isFiatCurrency(currencyCode))
return new Volume(Fiat.parseFiat(currencyCode, cleaned));
else
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/bisq/core/util/BSFormatter.java
Expand Up @@ -562,9 +562,15 @@ public long parsePriceStringToLong(String currencyCode, String amount, int preci
return value;
}

protected String cleanDoubleInput(String input) {
input = input.replace(",", ".");
public static String convertCharsForNumber(String input) {
// Some languages like finnish use the long dash for the minus
input = input.replace("−", "-");
input = StringUtils.deleteWhitespace(input);
return input.replace(",", ".");
}

protected String cleanDoubleInput(String input) {
input = convertCharsForNumber(input);
if (input.equals("."))
input = input.replace(".", "0.");
if (input.equals("-."))
Expand Down
Expand Up @@ -18,6 +18,7 @@
package bisq.desktop.util.validation;

import bisq.core.locale.Res;
import bisq.core.util.BSFormatter;
import bisq.core.util.validation.InputValidator;

/**
Expand All @@ -29,7 +30,7 @@
public abstract class NumberValidator extends InputValidator {

protected String cleanInput(String input) {
return input.replace(",", ".").trim();
return BSFormatter.convertCharsForNumber(input);
}

protected ValidationResult validateIfNumber(String input) {
Expand Down

0 comments on commit 108ce1d

Please sign in to comment.