Developer forum

Forum » Development » Currency format

Currency format

Anders Ebdrup
Reply

Hi!

 

I have a question about how to format a value to a currency. I have tried this:

 

var dwCurrency = new Dynamicweb.eCommerce.International.Currency("DKK");
template.SetTag("SmartPage:OrderLastYear.Total", dwCurrency.Format(1958.57));

But the last part: dwCurrency.Format() takes approx. 50 milliseconds, which seems to be unreasonable amount of time. I have a list of 50 orders, which should be outputted, but when formatting the currency it will take 2,5 second to output...

 

So how can I format the currency without having to worry about the performance?

 

Best regards, Anders


Replies

 
Nicolai Høeg Pedersen
Reply

Hm, the code is straight forward:

 

 

 

Public Function Format(ByVal value As Double) As String
            Return Format(value, True)
        End Function
        Public Function Format(ByVal value As Double, ByVal showSymbol As Boolean) As String
            Dim ci As System.Globalization.CultureInfo
            ci = Base.GetCulture()
            
            Dim FormatString As String = "#,##0."

            If Not Rounding Is Nothing AndAlso Rounding.ID <> "" Then
                For I As Integer = 1 To Rounding.Decimals
                    FormatString += "0"
                Next
            Else
                FormatString += "00"
            End If

            If showSymbol Then
                If SymbolPlace = 0 Then
                    Return Symbol & value.ToString(FormatString, ci)
                Else
                    Return value.ToString(FormatString, ci) & Symbol
                End If
            Else
                Return value.ToString(FormatString, ci)
            End If
        End Function

Cannot see where the bad performance should come from.

 

It might be the call to Base.GetCulture maybe?

 

Nicolai

 
Anders Ebdrup
Reply

Hi Nicolai,

 

I have tested: Base.GetCulture() and it is not the problem. The problem seems to be with "Rounding", which is not cached and is reloaded from the database on every instance.

 

public Rounding Rounding
    {
      get
      {
        if (this._Rounding == null)
          this._Rounding = new Rounding(this.RoundingID);
        return this._Rounding;
      }
      set
      {
        if (this._IsReadOnly)
          throw new ReadOnlyException();
        this._Rounding = Value;
      }
    }

My problem is that the currency can change from line to line in my for each loop, but with this I will need to cache the different instances.

 

/Anders

 
Anders Ebdrup
Reply

With this approach it is now possible for me the minimize the total time to 79 milliseconds instead for approx. 2500 milliseconds

 

                Dictionary<string, Dynamicweb.eCommerce.International.Currency> currencies = new Dictionary<string, Currency>();

                foreach (var ordersLastYear in orders)
                {
                    Currency dwCurrency;
                    if (!currencies.ContainsKey(ordersLastYear.CurrencyCode))
                    {
                        dwCurrency = new Dynamicweb.eCommerce.International.Currency(ordersLastYear.CurrencyCode);
                        currencies.Add(ordersLastYear.CurrencyCode, dwCurrency);
                    }
                    else
                    {
                        dwCurrency = currencies[ordersLastYear.CurrencyCode];
                    }
                    templateLoop.SetTag("SmartPage:OrderLastYear.Total", dwCurrency.Format(ordersLastYear.OrderTotal));

                    templateLoop.CommitLoop();
                }

Thank you for the quick reply, Nicolai, at first I thought that I was using the wrong method :-)

 

Best regards, Anders

 
Nicolai Høeg Pedersen
Reply

Hi Anders

 

Thank you. I'll register this as a needed improvement on the API.

 

BR Nicolai

 
Hans-Henrik Stefansen
Reply
This post has been marked as an answer

Hi Anders,

In 8.6.0.2 we have added caching to the Format-method to improve the performance.
The cache is culture info dependent.

Thanks for the input.

Best regards
Hans-Henrik

Votes for this answer: 1

 

You must be logged in to post in the forum