Search code examples
internationalizationdecimalodatasapui5

Converting OData V2 Edm.Decimal value to a locale-dependent decimal number in JS


I'm trying to convert a string number and display it as a decimal number. For example: "3125000.000"3,125,000.00.

In the XML I wrote:

<Text xmlns="sap.m"
  id="text13"
  text="{
    path: 'Amount',
    type: 'sap.ui.model.odata.type.Decimal',
    constraints: {
      scale: 2
    }
  }"
/>

It works well. How can I display that value from the Controller.js?

It doesn't matter, whether the converted value is of type string or number. I just want to display that value with the commas and the .00.


Solution

  • I think this documentation answers your question (if I understood it correctly)

    From documentation:

    var oFormatOptions = {
      minIntegerDigits: 3,
      maxIntegerDigits: 5,
      minFractionDigits: 2,
      maxFractionDigits: 4
    };
    
    // NumberFormat required from "sap/ui/core/format/NumberFormat"
    var oFloatFormat = NumberFormat.getFloatInstance(oFormatOptions);
    oFloatFormat.format(1.1); // returns 001.10
    oFloatFormat.format(1234.567); // returns 1,234.567