How to convert decimal number into integer using DataWeave

Last Updated on 03/05/2021 by Patryk Bandurski

This article is about converting decimal numbers into integer ones. This may seem tricky at first. You may say that we do not need to do anything special and the DataWeave engine will handle it underneath. However, there is a nuance that you should be aware of. In transformation to XML, this may not actually work. This tip is primarily dedicated to DataWeave 1.0 as in DataWeave 2.x this does not occur.

DataWeave data types

Below you can see available data types that you can use in DataWeave 1.0 and 2.0

  • Array – ordered sequence of elements either evaluated or literals
["Item1", "Item2", "2.0" as :number]
  • Object – key/value collection
{
  Key1: "Value",
  Key2: "2.0" as :number
}
  • String – text enclosed with quotations
"Simple String"
  • Number – floating point or integer value
 1.5 or 1
  • Boolean –  logical value true either false
true or false
  • Date – date or/and Time. Similar to newly introduced in Java 8 date and time types from java.time package. Date literals we enclose always with pipe character|
|2017-03-27|
|2003-10-01T23:57:59|

More you can find in Mule documenation for DataWeave 1.0 and DataWeave 2.0.

Contract

We have an array of Transactions witch TransactionValue in Euro. I would like to transform it into an XML array where each value is in Cents.

Input

{
  "ID": 5,
  "Transactions": [
    {
      "TransactionValue": 1330.5
    },
    {
      "TransactionValue": 1330
    },
    {
      "TransactionValue": 999.99
    },
    {
      "TransactionValue": 1000.0
    }
  ]
}

Output

<?xml version='1.0' encoding='windows-1250'?>
<Transactions>
  <ID>5</ID>
  <Values>
    <Value>133050</Value>
    <Value>133000</Value>
    <Value>99999</Value>
    <Value>100000</Value>
  </Values>
</Transactions>

Default behavior

Below transformation multiplies each TransactionValue by a hundred. As I mentioned earlier I accept decimal values however I need to have integers after transformation.

DataWeave 1.0 transformation

We receive an unexpected result:

<?xml version='1.0' encoding='windows-1250'?>
<Transactions>
  <ID>5</ID>
  <Values>
    <Value>133050.0</Value>
    <Value>133000</Value>
    <Value>99999.00</Value>
    <Value>100000.0</Value>
  </Values>
</Transactions>

As you may have noticed some values are floating-point. However, I was expecting integers. The value that had decimal points would be left as is, without transforming to an integer.  While in conversion to Json, values would be transformed to integers.

But still I want to have an XML output.

Casting to integer

Fix is fairly simple we need to add floor operator like below

Value: floor transaction.TransactionValue * 100

After that small change I have received output that I was expecting:

Transformation output

Summary

In conclusion when we would like to transform floating-point numbers into integers and we are transforming to XML we need to use the floor operator to remove decimal points. This obstacle does not occur in DataWeave 2.0.

How to convert decimal number into integer using DataWeave

One thought on “How to convert decimal number into integer using DataWeave

Leave a Reply

Your email address will not be published.

Scroll to top