Search code examples
javascriptsapui5formatter

Using a Formatter for the Currencies in SAPUI5


I want to build my own formattor for displaying the amount as it should in the different currencies.

One could guess I use this solution I already know:

                                               <t:template>
                                <Text text="{
                                                parts: [
                                                    {path: 'amount'},
                                                    {path: 'currency'}
                                                ],
                                                type:'sap.ui.model.type.Currency',
                                                formatOptions: {
                                                    currencyCode: false
                                                }
                                            }"
             </t:template>

the problem with this solution is I already show the currency in a seperate column and if I go with this solution it looks pretty ugly....

so I tried this one:

<t:template>
                                <Text text="{parts: [                
                                {path: 'amount'},
                                {path: 'currency'}
                                ],               
                                formatter : '.formatter.currency'}"
                                 />
                            </t:template>

and my formatter function looks like this:

    currency: function(amount, currency) {

        var change = [];
        change.push(amount);
        change.push(currency);
        var sInternalType = "";
        var amount1 = new sap.ui.model.type.Currency();


            amount1.formatValue(change, sInternalType);
            return amount1;
    }

Here I would guess I do something completely wrong, as English is not my first language I would probably assume I didnt understood the API References right, as they is stated this:

  • formatValue(vValue, sInternalType): any
  • Format the given array containing amount and currency code to an output value of type string. Other internal types than 'string' are not supported by the Currency type. If an source format is has been defined for this type, the formatValue does also accept a string value as input, which will be parsed into an array using the source format. If aValues is not defined or null, null will be returned.
  • Parameters:
  • {array|string} vValue the array of values or string value to be formatted
  • {string} sInternalType the target type
  • Returns:
  • {any} the formatted output value

Solution

  • If it is your intention not to show the currency symbol or code because you already show it elsewhere, you could simply set showMeasure to false, e.g.:

    <Text xmlns:core="sap.ui.core"
      core:require="{ CurrencyType: 'sap/ui/model/type/Currency' }"
      text="{
        parts: [
          'amount',
          'currency'
        ],
        type: 'CurrencyType',
        formatOptions: {
          showMeasure: false
        }
      }"
    />
    

    Not showing the currency code/symbol is a feature of the standard Currency type. You don't need to extend it.


    Note: In case of OData V4, require the type sap/ui/model/odata/type/Currency instead.