2

I want to convert a decimal (78.22) to an integer (78) using Dataweave.

<root>
<MoneyAmountRequired>78.22</MoneyAmountRequired>
</root>

I try as below and it doesn't work if I add default 0

moneyAmountRequired: payload.root.MoneyAmountRequired as :number{format: "0"} default 0 

Can some one please point out why it doesn't work when default 0 is present.

Thank you!

Neil24
  • 41
  • 4
  • 10

8 Answers8

2

Use floor function of dataweave.

{
    root: {
        MoneyAmountRequired: floor payload.root.MoneyAmountRequired
    }
}
Venu
  • 19
  • 3
0

You can try the following :-

moneyAmountRequired: payload.root.MoneyAmountRequired as :string {format: "0"}as :number default 0

Here you need to convert it first in String as :string then in number as :number
This is the only way it seems to work !!
ref:- Mule Dataweave format :number

Community
  • 1
  • 1
Anirban Sen Chowdhary
  • 8,233
  • 6
  • 39
  • 81
  • The problem is having default 0. I want to have a default value if the value is not present but if I keep it, I can't convert decimal to integer. as :number {format: "0"} works perfectly fine without default 0 – Neil24 May 18 '16 at 08:22
0

Try myvalue: removeDecimal(28.23)

Satheesh Kumar
  • 797
  • 7
  • 31
0

This works for me.

moneyAmountRequired: payload.root.MoneyAmountRequired as :number {format: "##,##"} as :string {format: "##"} as :number

Hope this helps.

AnupamBhusari
  • 2,395
  • 2
  • 14
  • 22
0

Check this: %dw 1.0 %output application/java

%var regex = /(.\d+?)0+\b/

data replace regex with ""

himanshu
  • 21
  • 2
  • 8
  • 3
    Hi, do add a bit of explanation along with the code as it helps to understand your code. Code only answers are frowned upon. – Bhargav Rao Sep 16 '16 at 20:41
0
((payload.root.MoneyAmountRequired as :string) splitBy '.')[0] as :number

If you prefer to use a function, then:

%function truncateDecimals(d) ((d as :string) splitBy '.')[0] as :number when d != null otherwise d
---
{value: truncateDecimals(payload.root.MoneyAmountRequired)}
0

After the number is formatted, it is indeed a string, but you are defaulting it to a number 0 instead of defaulting it to a string "0". But that would be enough if 'as :number' did not failed when the variable is null, which is in the first place the reason you need the default. Therefore, default it before converting it to a number as follows:

moneyAmountRequired: (payload.root.MoneyAmountRequired default "0") as :number{format: "0"}

Hope this will answer your question about the default 0 not working.

-1

Use this for

%dw 1.0

%output application/json

{ value : moneyAmountRequired: floor payload.root.MoneyAmountRequired as :number default 0 }

Zahid
  • 59
  • 10