Sunday, April 3, 2022

How to change currency symbol in a given number format

Say, we need to print a Vendor payment advice in the vendor's language, whic is es (Spanish) in the example below. Once the report parameter AX_RenderingCulture is set to 'es', all related number formatting will be applied to amounts cells. 

However, the payment may be made in different currencies; thus its currency symbol $ must be used instead of Euro.


Basically, such parameters, like currency symbol etc, can be changed through System.Globalization.CultureInfo class created for the rendering culture. I did not find a way how to achieve it for a particular textbox in SSRS design. So, I formatted the amount directly in X++.


I used Global::strFmtByLanguage method as a basis for my method to replace the culture number format currency symbol to a given one. There are a few other interesting methods you can check to see how to deal with formatting dates and numbers.

    /// <summary>
    /// Replaces language rendering culture currency symbol with the given one
    /// </summary>
    /// <param name = "_languageId">language rendering culture</param>
    /// <param name = "_amountCur">AmountCur</param>
    /// <param name = "_currencySymbol">New currency symbol</param>
    /// <returns>Formatted string</returns>
    public str eogChangeCurSymbolForAmountStr(LanguageId _languageId, System.Double _amountCur, CurrencySymbol _currencySymbol)
    {
        System.Globalization.CultureInfo    culture;
        str                                 res;
        System.Exception                    e;
        str                                 curSymbol;

        culture = new System.Globalization.CultureInfo(_languageId);

        try
        {
            res         = _amountCur.ToString("C", culture);
            curSymbol   = culture.NumberFormat.CurrencySymbol;
            res         = strReplace(res, curSymbol, _currencySymbol);
        }
        catch(Exception::CLRError)
        {
            e = CLRInterop::getLastException();
            while( e )
            {
                error( e.get_Message() );
                e = e.get_InnerException();
            }
            throw Exception::Error;
        }
        return res;
    }

    /// <summary>
    /// Gets amount with currency symbol
    /// </summary>
    /// <param name = "_amountCur">Amount in currency</param>
    /// <param name = "_currency">Currency to present symbol</param>
    /// <param name = "_languageId">Language for rendering culture</param>
    /// <returns>Formatted string for amunt cur with currency symbol</returns>
    public EOGAmountStringWithCurrencySymbol eogAmountStringWithCurrencySymbol(AmountCur _amountCur, CurrencyCode _currency, LanguageId _languageId)
    {
        Currency                            currency = Currency::find(_currency);
        
        return this.eogChangeCurSymbolForAmountStr(_languageId, _amountCur, currency.Symbol);;
    }

    /// <summary>
    /// Populates <c>BankPaymAdviceVendTmp</c> and inserts the report information for a single invoice and related payment.
    /// </summary>
    protected void insertBankPaymAdviceTmp()
    {
        BankPaymAdviceVendTmp bankPaymAdviceVendTmp;
        str                   email;

        next insertBankPaymAdviceTmp();

        bankPaymAdviceVendTmp = this.bankPaymAdviceTmp as BankPaymAdviceVendTmp;
                
        if (bankPaymAdviceVendTmp.RecId)
        {          
            ttsbegin;
            bankPaymAdviceVendTmp.selectForUpdate(true);
            bankPaymAdviceVendTmp.EOGBalance01Total+=bankPaymAdviceVendTmp.Balance01;
            bankPaymAdviceVendTmp.EOGAmountStringWithCurrencySymbol  = this.eogAmountStringWithCurrencySymbol(bankPaymAdviceVendTmp.EOGBalance01Total, bankPaymAdviceVendTmp.CurrencyCode, VendTable::find(bankPaymAdviceVendTmp.AccountNum).languageId());          
            bankPaymAdviceVendTmp.update();
            ttscommit;
        }
    }

The final string can be referenced in Total textbox as Last(bankPaymAdviceVendTmp.EOGAmountStringWithCurrencySymbol) and with default format.






No comments: