Add decimal point alignment

This commit is contained in:
Christoph Atteneder 2018-02-07 11:43:09 +01:00
parent c11246a1ee
commit c2c5760709
No known key found for this signature in database
GPG Key ID: CD5DC1C529CDFD3B
5 changed files with 120 additions and 2 deletions

View File

@ -36,6 +36,7 @@ import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Monetary;
import org.bitcoinj.utils.Fiat;
import org.bitcoinj.utils.MonetaryFormat;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -251,6 +252,24 @@ public class BSFormatter {
// Volume
///////////////////////////////////////////////////////////////////////////////////////////
public String formatVolume(Volume volume, Boolean decimalAligned) {
String formattedVolume = formatVolume(volume);
if(decimalAligned) {
formattedVolume = fillUpPlacesWithEmptyStrings(formattedVolume, 5);
}
return formattedVolume;
}
@NotNull
private String fillUpPlacesWithEmptyStrings(String formattedVolume, int maxNumberOfDigits) {
int numberOfPlacesToFill = maxNumberOfDigits - formattedVolume.split("\\.")[0].length();
for (int i = 0; i < numberOfPlacesToFill; i++) {
formattedVolume = " " + formattedVolume;
}
return formattedVolume;
}
public String formatVolume(Volume volume) {
return formatVolume(volume, fiatVolumeFormat, false);
}
@ -301,7 +320,6 @@ public class BSFormatter {
return Res.get("formatter.formatVolumeLabel",
currencyCode, postFix);
}
public String formatMinVolumeAndVolume(Offer offer) {
return offer.isRange() ? formatVolume(offer.getMinVolume()) + " - " + formatVolume(offer.getVolume()) : formatVolume(offer.getVolume());
}
@ -341,6 +359,15 @@ public class BSFormatter {
return formatPrice(price, fiatPriceFormat, false);
}
public String formatPrice(Price price, Boolean decimalAligned) {
String formattedPrice = formatPrice(price);
if(decimalAligned) {
formattedPrice = fillUpPlacesWithEmptyStrings(formattedPrice, 5);
}
return formattedPrice;
}
public String formatPriceWithCode(Price price) {
Monetary monetary = price.getMonetary();
if (monetary instanceof Fiat)

View File

@ -0,0 +1,23 @@
package io.bisq.common.monetary;
import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Maker;
import com.natpryce.makeiteasy.Property;
import org.bitcoinj.utils.Fiat;
import static com.natpryce.makeiteasy.MakeItEasy.a;
public class PriceMaker {
public static final Property<Price, String> currencyCode = new Property<>();
public static final Property<Price, String> priceString = new Property<>();
public static final Instantiator<Price> FiatPrice = lookup ->
new Price(Fiat.parseFiat(lookup.valueOf(currencyCode, "USD"), lookup.valueOf(priceString, "100")));
public static final Instantiator<Price> AltcoinPrice = lookup ->
new Price(Altcoin.parseAltcoin(lookup.valueOf(currencyCode, "LTC"), lookup.valueOf(priceString, "100")));
public static final Maker<Price> usdPrice = a(FiatPrice);
public static final Maker<Price> ltcPrice = a(AltcoinPrice);
}

View File

@ -0,0 +1,23 @@
package io.bisq.common.monetary;
import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Maker;
import com.natpryce.makeiteasy.Property;
import org.bitcoinj.utils.Fiat;
import static com.natpryce.makeiteasy.MakeItEasy.a;
public class VolumeMaker {
public static final Property<Volume, String> currencyCode = new Property<>();
public static final Property<Volume, String> volumeString = new Property<>();
public static final Instantiator<Volume> FiatVolume = lookup ->
new Volume(Fiat.parseFiat(lookup.valueOf(currencyCode, "USD"), lookup.valueOf(volumeString,"100")));
public static final Instantiator<Volume> AltcoinVolume = lookup ->
new Volume(Altcoin.parseAltcoin(lookup.valueOf(currencyCode, "LTC"), lookup.valueOf(volumeString, "100")));
public static final Maker<Volume> usdVolume = a(FiatVolume);
public static final Maker<Volume> ltcVolume = a(AltcoinVolume);
}

View File

@ -31,13 +31,20 @@ import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static io.bisq.common.monetary.PriceMaker.priceString;
import static io.bisq.common.monetary.PriceMaker.usdPrice;
import static io.bisq.common.monetary.VolumeMaker.usdVolume;
import static io.bisq.common.monetary.VolumeMaker.volumeString;
import static org.bitcoinj.core.CoinMaker.oneBitcoin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Offer.class,OfferPayload.class})
@PrepareForTest({Offer.class, OfferPayload.class})
public class BSFormatterTest {
private BSFormatter formatter;
@ -79,6 +86,25 @@ public class BSFormatterTest {
assertTrue(formatter.formatDurationAsWords(0).isEmpty());
}
@Test
public void testFormatPrice() {
assertEquals("100.0000", formatter.formatPrice(make(usdPrice)));
assertEquals(" 100.0000", formatter.formatPrice(make(usdPrice), true));
assertEquals("7098.4700", formatter.formatPrice(make(usdPrice.but(with(priceString, "7098.4700")))));
}
@Test
public void testFormatCoin() {
assertEquals("1.00", formatter.formatCoin(oneBitcoin));
}
@Test
public void testFormatVolume() {
assertEquals(" 100.00", formatter.formatVolume(make(usdVolume), true));
assertEquals("100.00", formatter.formatVolume(make(usdVolume)));
assertEquals("1774.62", formatter.formatVolume(make(usdVolume.but(with(volumeString, "1774.62")))));
}
@Test
public void testFormatSameVolume() {
Offer offer = mock(Offer.class);

View File

@ -0,0 +1,19 @@
package org.bitcoinj.core;
import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Property;
import static com.natpryce.makeiteasy.MakeItEasy.a;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static org.bitcoinj.core.Coin.*;
public class CoinMaker {
private static final Property<Coin, Long> satoshis = new Property<>();
public static final Instantiator<Coin> Coin = lookup ->
valueOf(lookup.valueOf(satoshis, 100000000L));
public static final Coin oneBitcoin = make(a(Coin));
}