package common.utils;

import io.sphere.sdk.models.Base;
import org.javamoney.moneta.format.CurrencyStyle;

import javax.money.MonetaryAmount;
import javax.money.format.AmountFormatQuery;
import javax.money.format.AmountFormatQueryBuilder;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;
import java.util.Locale;

final class PriceFormatterImpl extends Base implements PriceFormatter {

    private final Locale locale;

    PriceFormatterImpl(final Locale locale) {
        this.locale = locale;
    }

    @Override
    public String format(MonetaryAmount monetaryAmount) {
        final AmountFormatQuery amountFormatQuery = AmountFormatQueryBuilder.of(locale)
                .set("pattern", "###0.00### ¤")
                .set(CurrencyStyle.SYMBOL)
                .build();
        final MonetaryAmountFormat amountFormat = MonetaryFormats.getAmountFormat(amountFormatQuery);
        return amountFormat.format(monetaryAmount)
                .replace("EUR", "€");//yep, the money lib does not know the euro sign
    }
}

@@ -20,42 +20,8 @@
 s;%0A%0A
-import io.sphere.sdk.models.Base;%0A
 impo
@@ -67,16 +67,50 @@
 Style;%0A%0A
+import javax.annotation.Nullable;%0A
 import j
@@ -237,56 +237,8 @@
 er;%0A
-import javax.money.format.MonetaryAmountFormat;%0A
 impo
@@ -305,14 +305,113 @@
 le;%0A
-%0Afinal
+import java.util.Optional;%0A%0Aimport static org.javamoney.moneta.format.AmountFormatParams.PATTERN;%0A%0Apublic
  cla
@@ -436,21 +436,8 @@
 mpl 
-extends Base 
 impl
@@ -460,16 +460,163 @@
 atter %7B%0A
+    private static final String PATTERN_WITHOUT_DECIMAL = %22%C2%A4 #,###,##0.-%22;%0A    private static final String PATTERN_WITH_DECIMAL = %22%C2%A4 #,###,##0.00%22;
 %0A    pri
@@ -768,74 +768,248 @@
 mat(
-MonetaryAmount monetaryAmount) %7B%0A        final AmountFormatQuery a
+@Nullable final MonetaryAmount price) %7B%0A        return Optional.ofNullable(price).map(p -%3E %7B%0A            final boolean isDecimal = p.getNumber().doubleValueExact() %25 1 != 0;//TODO this can be improved with monetary query%0A            final A
 moun
@@ -1021,16 +1021,24 @@
 atQuery 
+pattern 
 = Amount
@@ -1087,347 +1087,256 @@
     
+    
 .set(
-%22pattern%22, %22###0.00### %C2%A4%22)%0A                .set(CurrencyStyle.SYMBOL)%0A                .build();%0A        final MonetaryAmountFormat amountFormat = MonetaryFormats.getAmountFormat(amountFormatQuery);%0A        return amountFormat.format(monetaryAmount)%0A                .replace(%22EUR%22, %22%E2%82%AC%22);//yep, the money lib does not know the euro sign
+CurrencyStyle.SYMBOL)%0A                    .set(PATTERN, isDecimal ? PATTERN_WITH_DECIMAL : PATTERN_WITHOUT_DECIMAL)%0A                    .build();%0A            return MonetaryFormats.getAmountFormat(pattern).format(p);%0A        %7D).orElse(%22%22);
 %0A   

Use PriceFormatterImpl from Sunrise Dmexco.