简体   繁体   English

在 JS 中将 OData V2 Edm.Decimal 值转换为与语言环境相关的十进制数

[英]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 .例如: "3125000.000"3,125,000.00

In the XML I wrote:在 XML 中,我写道:

<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?如何从 Controller.js 显示该值?

It doesn't matter, whether the converted value is of type string or number .转换后的值是string类型还是number类型并不重要。 I just want to display that value with the commas and the .00 .我只想用逗号和.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:可以在 JS 中实现与数据类型的属性绑定,类似于 XML 变体,通过将类型分配给type但在其构造函数中传递设置:

Constructor for a primitive type Edm.Decimal .基本类型Edm.Decimal构造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 API 参考: 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" .如果用户的当前语言环境是例如de-DE ,则返回值将是"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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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