mirror of
https://github.com/bisq-network/bisq.git
synced 2025-03-03 18:56:59 +01:00
Do some minor cleanup of DisplayUtils
This commit is contained in:
parent
fab37f9f01
commit
6d39bc0894
1 changed files with 26 additions and 25 deletions
|
@ -23,6 +23,7 @@ import org.apache.commons.lang3.time.DurationFormatUtils;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -31,8 +32,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class DisplayUtils {
|
public class DisplayUtils {
|
||||||
private final static int scale = 3;
|
private static final int SCALE = 3;
|
||||||
private static final MonetaryFormat fiatVolumeFormat = new MonetaryFormat().shift(0).minDecimals(0).repeatOptionalDecimals(0, 0);
|
private static final MonetaryFormat FIAT_VOLUME_FORMAT = new MonetaryFormat().shift(0).minDecimals(0).repeatOptionalDecimals(0, 0);
|
||||||
|
|
||||||
public static String formatDateTime(Date date) {
|
public static String formatDateTime(Date date) {
|
||||||
return FormattingUtils.formatDateTime(date, true);
|
return FormattingUtils.formatDateTime(date, true);
|
||||||
|
@ -96,7 +97,7 @@ public class DisplayUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatVolume(Volume volume) {
|
public static String formatVolume(Volume volume) {
|
||||||
return formatVolume(volume, fiatVolumeFormat, false);
|
return formatVolume(volume, FIAT_VOLUME_FORMAT, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String formatVolume(Volume volume, MonetaryFormat fiatVolumeFormat, boolean appendCurrencyCode) {
|
private static String formatVolume(Volume volume, MonetaryFormat fiatVolumeFormat, boolean appendCurrencyCode) {
|
||||||
|
@ -112,7 +113,7 @@ public class DisplayUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatVolumeWithCode(Volume volume) {
|
public static String formatVolumeWithCode(Volume volume) {
|
||||||
return formatVolume(volume, fiatVolumeFormat, true);
|
return formatVolume(volume, FIAT_VOLUME_FORMAT, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatVolumeLabel(String currencyCode) {
|
public static String formatVolumeLabel(String currencyCode) {
|
||||||
|
@ -205,16 +206,10 @@ public class DisplayUtils {
|
||||||
// Amount
|
// Amount
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
public static String formatAmount(Offer offer, CoinFormatter formatter) {
|
public static String formatAmount(Offer offer, CoinFormatter coinFormatter) {
|
||||||
return formatAmount(offer, false, formatter);
|
return offer.isRange()
|
||||||
}
|
? coinFormatter.formatCoin(offer.getMinAmount()) + FormattingUtils.RANGE_SEPARATOR + coinFormatter.formatCoin(offer.getAmount())
|
||||||
|
: coinFormatter.formatCoin(offer.getAmount());
|
||||||
private static String formatAmount(Offer offer, boolean decimalAligned, CoinFormatter coinFormatter) {
|
|
||||||
String formattedAmount = offer.isRange() ? coinFormatter.formatCoin(offer.getMinAmount()) + FormattingUtils.RANGE_SEPARATOR + coinFormatter.formatCoin(offer.getAmount()) : coinFormatter.formatCoin(offer.getAmount());
|
|
||||||
if (decimalAligned) {
|
|
||||||
formattedAmount = FormattingUtils.fillUpPlacesWithEmptyStrings(formattedAmount, 15);
|
|
||||||
}
|
|
||||||
return formattedAmount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatAmount(Offer offer,
|
public static String formatAmount(Offer offer,
|
||||||
|
@ -222,7 +217,9 @@ public class DisplayUtils {
|
||||||
boolean decimalAligned,
|
boolean decimalAligned,
|
||||||
int maxPlaces,
|
int maxPlaces,
|
||||||
CoinFormatter coinFormatter) {
|
CoinFormatter coinFormatter) {
|
||||||
String formattedAmount = offer.isRange() ? coinFormatter.formatCoin(offer.getMinAmount(), decimalPlaces) + FormattingUtils.RANGE_SEPARATOR + coinFormatter.formatCoin(offer.getAmount(), decimalPlaces) : coinFormatter.formatCoin(offer.getAmount(), decimalPlaces);
|
String formattedAmount = offer.isRange()
|
||||||
|
? coinFormatter.formatCoin(offer.getMinAmount(), decimalPlaces) + FormattingUtils.RANGE_SEPARATOR + coinFormatter.formatCoin(offer.getAmount(), decimalPlaces)
|
||||||
|
: coinFormatter.formatCoin(offer.getAmount(), decimalPlaces);
|
||||||
|
|
||||||
if (decimalAligned) {
|
if (decimalAligned) {
|
||||||
formattedAmount = FormattingUtils.fillUpPlacesWithEmptyStrings(formattedAmount, maxPlaces);
|
formattedAmount = FormattingUtils.fillUpPlacesWithEmptyStrings(formattedAmount, maxPlaces);
|
||||||
|
@ -258,17 +255,21 @@ public class DisplayUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts to a coin with max. 4 decimal places. Last place gets rounded.
|
* Converts to a coin with max. 4 decimal places. Last place gets rounded.
|
||||||
* 0.01234 -> 0.0123
|
* <p>0.01234 -> 0.0123
|
||||||
* 0.01235 -> 0.0124
|
* <p>0.01235 -> 0.0124
|
||||||
*
|
*
|
||||||
* @param input
|
* @param input the decimal coin value to parse and round
|
||||||
* @param coinFormatter
|
* @param coinFormatter the coin formatter instance
|
||||||
* @return
|
* @return the converted coin
|
||||||
*/
|
*/
|
||||||
public static Coin parseToCoinWith4Decimals(String input, CoinFormatter coinFormatter) {
|
public static Coin parseToCoinWith4Decimals(String input, CoinFormatter coinFormatter) {
|
||||||
try {
|
try {
|
||||||
return Coin.valueOf(new BigDecimal(ParsingUtils.parseToCoin(ParsingUtils.cleanDoubleInput(input), coinFormatter).value).setScale(-scale - 1,
|
return Coin.valueOf(
|
||||||
BigDecimal.ROUND_HALF_UP).setScale(scale + 1, BigDecimal.ROUND_HALF_UP).toBigInteger().longValue());
|
new BigDecimal(ParsingUtils.parseToCoin(ParsingUtils.cleanDoubleInput(input), coinFormatter).value)
|
||||||
|
.setScale(-SCALE - 1, RoundingMode.HALF_UP)
|
||||||
|
.setScale(SCALE + 1, RoundingMode.HALF_UP)
|
||||||
|
.toBigInteger().longValue()
|
||||||
|
);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (input != null && input.length() > 0)
|
if (input != null && input.length() > 0)
|
||||||
log.warn("Exception at parseToCoinWith4Decimals: " + t.toString());
|
log.warn("Exception at parseToCoinWith4Decimals: " + t.toString());
|
||||||
|
@ -283,9 +284,9 @@ public class DisplayUtils {
|
||||||
/**
|
/**
|
||||||
* Transform a coin with the properties defined in the format (used to reduce decimal places)
|
* Transform a coin with the properties defined in the format (used to reduce decimal places)
|
||||||
*
|
*
|
||||||
* @param coin The coin which should be transformed
|
* @param coin the coin which should be transformed
|
||||||
* @param coinFormatter
|
* @param coinFormatter the coin formatter instance
|
||||||
* @return The transformed coin
|
* @return the transformed coin
|
||||||
*/
|
*/
|
||||||
public static Coin reduceTo4Decimals(Coin coin, CoinFormatter coinFormatter) {
|
public static Coin reduceTo4Decimals(Coin coin, CoinFormatter coinFormatter) {
|
||||||
return ParsingUtils.parseToCoin(coinFormatter.formatCoin(coin), coinFormatter);
|
return ParsingUtils.parseToCoin(coinFormatter.formatCoin(coin), coinFormatter);
|
||||||
|
|
Loading…
Add table
Reference in a new issue