How do I add the Litium logic for currency for a decimal value

We have a price that is “price without vat”. And that price I need to set with the decimal

So I can use it on items like this Model.CampaignPrice (CampaignPriceItem). But this is a price with vat. So if I want the price without vat I add Model.CampaignPrice.Price (decimal). But then I cant use the logic to get the format set in Litium.

Litium version: 6.2.2

If I understand you correctly your prices is including VAT and you want to use the inbuilt logic to display them?

If you in that case create an own price-item-class; example

    public class PriceItemWithVat : ProductPriceModel.PriceItem
    {
        public PriceItemWithVat(decimal minimumQuantity, decimal price, decimal vatPercentage)
            : base(minimumQuantity, decimal.Zero, vatPercentage)
        {
            PriceWithVat = price;
            Price = price / (1 + vatPercentage);
        }

        /// <summary>
        ///     Gets or sets the price.
        /// </summary>
        /// <value>The price.</value>
        public override decimal Price { get; set; }

        /// <summary>
        ///     Gets the price with vat.
        /// </summary>
        /// <value>The price with vat.</value>
        public override decimal PriceWithVat { get; }
    }

And then adding the following methods to the builder that is creating your price

        private string Format(decimal amount, bool displayCurrencySymbol, Currency currency)
        {
            NumberFormatInfo numberFormat;
            if (!displayCurrencySymbol)
            {
                numberFormat = (NumberFormatInfo)currency.GetNumberFormatInfo(CultureInfo.CurrentUICulture).Clone();
                numberFormat.CurrencySymbol = string.Empty;
            }
            else
            {
                numberFormat = currency.GetNumberFormatInfo(CultureInfo.CurrentUICulture);
            }
            return amount.ToString(currency.TextFormat, numberFormat).Trim();
        }

        private T SetFormattedPrice<T>(T item, Currency currency) where T : ProductPriceModel.PriceItem
        {
            var price = _cart.IncludeVAT ? item.PriceWithVat : item.Price;
            item.FormatPrice = x => Format(price, x, currency);
            return item;
        }

you can construct your prices like this

    var price = new PriceItemWithVat(0, 125, 0.25);
    SetFormattedPrice(price, currency);

And after that the rendering page should work without any modifications.

1 Like

Yes and no. I use the built in to display prices, but they are all with vat. So when I’m displaying it without vat there is no built in method in the accelerator to use the same logic that I use for thoose with vat.

For example:
I write this to get campaign price with vat:
@Model.ProductItem.Price.CampaignPrice.FormatPrice() in ProductWithVariants.cshtml
but without vat I need to write @Model.ProductItem.Price.CampaignPrice.FormatPrice.Price but then it becomes an decimal so I can use FormatPrice().

But still, I can use your answer for this anyway! :slight_smile: Thank you