Fix incorrect rounding of BSQ dollar price to whole number

Add a new method to DisplayUtils to restore the old rounding behaviour
of formatVolumeWithCode whenever a fractional volume is required. This
fixes a regression caused by #3926 to remove unnecessarily displayed
decimals for fiat volumes - it appears that in every case but the avg.
dollar price on the BSQ dashboard a whole number should be shown.

Also add a relevant test.
This commit is contained in:
Steven Barclay 2020-02-08 23:15:45 +00:00 committed by Christoph Atteneder
parent 6d39bc0894
commit 3cfb583e8a
No known key found for this signature in database
GPG key ID: CD5DC1C529CDFD3B
3 changed files with 25 additions and 6 deletions

View file

@ -116,6 +116,10 @@ public class DisplayUtils {
return formatVolume(volume, FIAT_VOLUME_FORMAT, true);
}
static String formatAverageVolumeWithCode(Volume volume) {
return formatVolume(volume, FIAT_VOLUME_FORMAT.minDecimals(2), true);
}
public static String formatVolumeLabel(String currencyCode) {
return formatVolumeLabel(currencyCode, "");
}

View file

@ -949,7 +949,7 @@ public class GUIUtil {
showBsqFeeInfoPopup(fee, miningFee, null, txSize, bsqFormatter, btcFormatter, type, actionHandler);
}
public static void setFitToRowsForTableView(TableView tableView,
public static void setFitToRowsForTableView(TableView<?> tableView,
int rowHeight,
int headerHeight,
int minNumRows,
@ -1100,7 +1100,7 @@ public class GUIUtil {
Volume bsqAmountAsVolume = Volume.parse(bsqAmountAsString, "BSQ");
Coin requiredBtc = bsqPrice.getAmountByVolume(bsqAmountAsVolume);
Volume volumeByAmount = usdPrice.getVolumeByAmount(requiredBtc);
return DisplayUtils.formatVolumeWithCode(volumeByAmount);
return DisplayUtils.formatAverageVolumeWithCode(volumeByAmount);
}
public static MaterialDesignIcon getIconForSignState(AccountAgeWitnessService.SignState state) {

View file

@ -22,8 +22,12 @@ import bisq.desktop.util.validation.RegexValidator;
import bisq.core.locale.GlobalSettings;
import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.monetary.Price;
import bisq.core.provider.price.MarketPrice;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.user.DontShowAgainLookup;
import bisq.core.user.Preferences;
import bisq.core.util.coin.BsqFormatter;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.CoinMaker;
@ -51,7 +55,6 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Ignore
public class GUIUtilTest {
@ -65,7 +68,7 @@ public class GUIUtilTest {
@Test
public void testTradeCurrencyConverter() {
Map<String, Integer> offerCounts = new HashMap<String, Integer>() {{
Map<String, Integer> offerCounts = new HashMap<>() {{
put("BTC", 11);
put("EUR", 10);
}};
@ -80,7 +83,7 @@ public class GUIUtilTest {
}
@Test
public void testOpenURLWithCampaignParameters() throws Exception {
public void testOpenURLWithCampaignParameters() {
Preferences preferences = mock(Preferences.class);
DontShowAgainLookup.setPreferences(preferences);
GUIUtil.setPreferences(preferences);
@ -101,7 +104,7 @@ public class GUIUtilTest {
}
@Test
public void testOpenURLWithoutCampaignParameters() throws Exception {
public void testOpenURLWithoutCampaignParameters() {
Preferences preferences = mock(Preferences.class);
DontShowAgainLookup.setPreferences(preferences);
GUIUtil.setPreferences(preferences);
@ -143,6 +146,18 @@ public class GUIUtilTest {
assertFalse(regexValidator.validate("12.34.56.78:").isValid);
}
@Test
public void testGetBsqInUsd() {
PriceFeedService priceFeedService = mock(PriceFeedService.class);
when(priceFeedService.getMarketPrice("USD"))
.thenReturn(new MarketPrice("USD", 12345.6789, 0, true));
Coin oneBsq = Coin.valueOf(100);
Price avgPrice = Price.valueOf("BSQ", 10000);
assertEquals("1.23 USD", GUIUtil.getBsqInUsd(avgPrice, oneBsq, priceFeedService, new BsqFormatter()));
}
@Test
public void percentageOfTradeAmount_higherFeeAsMin() {