Jump to content

Decimal Rounding Problem


Recommended Posts

Hi Folks, I am using XSLT 1.0, and stuck in a situation where I am having decimal rounding problem. Let me give you an example: My generated XML is:

<?xml version="1.0" encoding="UTF-8"?><CostSummary><CostingItems>  <CostingItem CostBasis="7" Description="Per Person" PassengerRPH="1">   <UnitCost Amount="3.333333" CurrencyCode="EUR"/>  </CostingItem>  <CostingItem CostBasis="7" Description="Per Person" PassengerRPH="2">   <UnitCost Amount="3.333333" CurrencyCode="EUR"/>  </CostingItem>  <CostingItem CostBasis="7" Description="Per Person" PassengerRPH="2">   <UnitCost Amount="3.333333" CurrencyCode="EUR"/>  </CostingItem></CostingItems><GrossAmount Amount="10.00" CurrencyCode="EUR"/></CostSummary>

My requirement is that the GrossAmount (10), should always be equal to the sum of all the UnitCost Amounts. In the above xml, each UnitCost Amount is generated by GrossAmount/No. of CostingItem >> 10/3= 3.33333. Hence in order to resolve this problem, I have decided to roundup the values to 2 decimal places and adjust the difference in the last CostingItem's UnitCost. For example:

<?xml version="1.0" encoding="UTF-8"?><CostSummary><CostingItems>  <CostingItem CostBasis="7" Description="Per Person" PassengerRPH="1">   <UnitCost Amount="3.34" CurrencyCode="EUR"/>  </CostingItem>  <CostingItem CostBasis="7" Description="Per Person" PassengerRPH="2">   <UnitCost Amount="3.34" CurrencyCode="EUR"/>  </CostingItem>  <CostingItem CostBasis="7" Description="Per Person" PassengerRPH="2">   <UnitCost Amount="3.32" CurrencyCode="EUR"/>  </CostingItem></CostingItems><GrossAmount Amount="10.00" CurrencyCode="EUR"/></CostSummary>

So could someone please help me out in creating the xslt script for the above scenario? I am currently using xslt 1.0. Thanks.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...