cancel
Showing results for 
Search instead for 
Did you mean: 

Formatter for Calculations

former_member257196
Participant
0 Kudos

Hi experts,

am new in using formatters,

i need to get value in percentage Example : 1600,1500 vales are there i need to get percentage value so 1600%1500*100 =106.6%

View.xml

<mvc:View controllerName="Formatter.controller.View1" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m">

  <App>

  <pages>

  <Page title="{i18n>title}">

  <content>

  <List headerText="Information" items="{path: '/results'}">

  <CustomListItem>

  <VBox class="sapUiSmallMarginBegin sapUiSmallMarginTopBottom">

  <HBox>

  <Label text="Sales"/>

  <Text text="{Sales}"/>

  </HBox>

  <HBox>

  <Label text="Goal"/>

  <Text text="{goal}"/>

  </HBox>

  <HBox>

  <Label text="Percentage"/>

  <!--<Text text="{parts: [{ path: '{Sales'},{ path: '{goal}'] formatter: 'formatter' }"/>-->

  </HBox>

  </VBox>

  </CustomListItem>

  </List>

  </content>

  </Page>

  </pages>

  </App>

</mvc:View>

formatter.js

sap.ui.define([], function () {

  "use strict";

  return {

  calculation: function (a,b) {

  return a*b%100;

  },

  };

});

controller.js

sap.ui.define([

  "sap/ui/core/mvc/Controller",

  'sap/ui/model/json/JSONModel',

], function(Controller, JSONModel) {

  "use strict";

  return Controller.extend("Formatter.controller.View1", {

    onInit: function() {

       var oModel = new JSONModel(jQuery.sap.getModulePath("Formatter.model", "/madhu.json"));

       this.getView().setModel(oModel);

  },

  });

});

Please help,

Thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

saivellanki
Active Contributor
0 Kudos

Hi Madhu,

Will this sample help? JS Bin - Collaborative JavaScript Debugging

Regards,

Sai.

former_member257196
Participant
0 Kudos

hi sai,

am using web ide i created formatter.js under model folder

sap.ui.define([

  ] , function () {

  "use strict";

  return {

  /**

  * Rounds the number unit value to 2 digits

  * @public

  * @param {string} sValue the number string to be rounded

  * @returns {string} sValue with 2 digits rounded

  */

  numberUnit : function (sValue) {

  if (!sValue) {

  return "";

  }

  return parseFloat(sValue).toFixed(2);

  },

  var Formatter = {

      Percentage: function(a, b) {

        var oCalculation = (parseInt(a) / parseInt(b)) * 100;

        return oCalculation.toFixed(2);

      }

    };

  };

  }

);

near var Formatter it showing unexpected identifier and do i need to add anything in controller

Former Member
0 Kudos

Hi

Use in this way

jQuery.sap.declare("retail.store.stockcorrection.YDMD_UI_COUNT.util.Formatter");

retail.store.stockcorrection.YDMD_UI_COUNT.util.Formatter = {

    removeLeadZeros: function(value) {

        if (value) {

            var removeZeros = value.replace(/^0+/, '');

            return removeZeros;

        } else {

            return value;

        }

    },

retail.store.stockcorrection.YDMD_UI_COUNT.util.Formatter in your folder stucture

former_member257196
Participant
0 Kudos

hi sai am having values as below in json

{

            Sales: "1,600.00",

            goal: "1,500.00"

          }, {

            Sales: "1,650.00",

            goal: "1,500.00"

          }

saivellanki
Active Contributor
0 Kudos

JS Bin edited: JS Bin - Collaborative JavaScript Debugging

Regards,

Sai.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi madhu ,

in the controller you need to tell that there is some formatter.js file which will format your data before attaching to view controls :

sap.ui.define([

  "sap/ui/core/mvc/Controller",

  'sap/ui/model/json/JSONModel',

"Formatter/util/Formatter"

], function(Controller, JSONModel,formatter) {

  "use strict";

  return Controller.extend("Formatter.controller.View1", {

   formatter: formatter,

     onInit: function() {

       var oModel = new JSONModel(jQuery.sap.getModulePath("Formatter.model", "/madhu.json"));

       this.getView().setModel(oModel);

  },

  });

});

and in the view please check your syntax .. it should be like this :

<Text text="{parts:[{path:'Sales'},{path:'goal'}],formatter:'.formatter.calculation'}"/>

your Formatter.js file is correct.

note:

my example above will find formatter file under util folder with name Formatter.js

thanks

Viplove