简体   繁体   中英

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 .

Property binding with data type can be achieved in JS, similar to the XML variant, by assigning the type to type but passing the settings in its constructor function instead:

Constructor for a primitive type Edm.Decimal .
new sap.ui.model.odata.type.Decimal(oFormatOptions?, oConstraints?)

new Text({
  text: {
    path: "Amount",
    type: new ODataDecimalType({ // required from "sap/ui/model/odata/type/Decimal"
      // Optional format options: ...
    }, {
      // Optional constraints:
      scale: 2
    }),
  }
});

Here is a demo:

 sap.ui.getCore().attachInit(() => sap.ui.require([ "sap/m/Title", "sap/ui/model/odata/type/Decimal", ], (Title, ODataTypeDecimal) => new Title({ text: { value: "3125000.000", // sample OData decimal value from the model type: new ODataTypeDecimal({}, { scale: 2 }), }, titleStyle: "H1", }).addStyleClass("sapUiTinyMargin").placeAt("content")));
 <script id="sap-ui-bootstrap" src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" data-sap-ui-libs="sap.ui.core,sap.m" data-sap-ui-theme="sap_fiori_3_dark" data-sap-ui-async="true" data-sap-ui-compatversion="edge" data-sap-ui-xx-waitfortheme="init" ></script><body id="content" class="sapUiBody"></body>


Data types can be also used outside of the property binding independently:

// ODataDecimalType required from "sap/ui/model/odata/type/Decimal"
const myType = new ODataDecimalType({}, { scale: 2 });
myType.formatValue("3125000.000", "string") // returns: "3,125,000.00"

API reference: sap.ui.model.odata.type.Decimal

If user's current locale is eg de-DE , the return value would be then "3.125.000,00" .


Note: the locale is automatically detected by the framework according to user's preference in their browser settings (See Identifying the Language Code / Locale ). I do not recommend hard-coding it unless it's absolutely required.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM