cancel
Showing results for 
Search instead for 
Did you mean: 

How to round a number with a data type float?

ferrygun18
Contributor

Hello all,

I got a float type value of "Amount" field in database 8,124.12 and when display this value in UI5, I got the value of 8,124.11

This is my UI5 code:

<Input
value="{path: 'AMOUNT', type: 'sap.ui.model.type.Float', formatOptions: {maxFractionDigits: 2, roundingMode: 'AWAY_FROM_ZERO', parseAsString: true, style: 'standard'}}"
valueLiveUpdate="false" maxLength="50" name="AMOUNT"/>

Amount data type definition:

{name = "AMOUNT"; sqlType = DOUBLE; comment = "Amount";},

And the value from OData:

How can I get the correct value in UI5 as in database record (which is 8,124.12 and not 8,124.11) ?

Regards,

Ferry

View Entire Topic
boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

If the entity property "AMOUNT" has a type of "Edm.Double" (check the $metadata document), the binding type 'sap.ui.model.odata.type.Double' has to be assigned in place of Float.

value="{
  path: 'AMOUNT',
  type: 'sap.ui.model.odata.type.Double',
  formatOptions: {
    maxFractionDigits: 2,
    roundingMode: 'AWAY_FROM_ZERO'
  }
}"

Keep in mind that the value of "roundingMode" needs to be in lower case if the target SAPUI5 version is lower than 1.85.* I.e. 'AWAY_FROM_ZERO' → 'away_from_zero'.

___

* Related issue: https://github.com/SAP/openui5/issues/2169
See also https://stackoverflow.com/a/43946624/5846045

ferrygun18
Contributor

Thank you boghyon.hoffmann ! It worked as expected.

former_member735290
Discoverer
0 Kudos

Hello boghyon.hoffmann,

For JSON Model also we can do the same process? or is there any alternative can you please help me with this.

Thanks in advance.

former_member34
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi there,

You have a question and need help by the community? Instead of posting into an old question thread, it is more helpful for you, if you create your own question. Here is how to get started:

  1. Learn about asking and answering questions in SAP Community with this tutorial: https://developers.sap.com/tutorials/community-qa.html
  2. Ask your detailed question here: https://answers.sap.com/questions/ask.html
  3. Wait for a response.

That's it. Thank you!

Best regards

Your SAP Community moderator

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

sshanmuka Does this help? --> https://embed.plnkr.co/b32woxbQmdj5dTRu?show=view/Root.view.xml,preview:?sap-ui-xx-componentPreload=...

With a client side model (e.g. JSONModel), 'sap.ui.model.type.Float' can be used, instead of 'sap.ui.model.odata.type.Double'.