Search code examples
trimdataweavemule4anypoint-studio

Mule4 - Dataweave - Remove Leading zeros


My postman output payload always returns with leading zeros for the field "NewPrice". I need assistance with making this field 2 decimal places.

Current Output Payload: [ { "EndDate": "1900-01-01", "NewPrice": 135.3000, "StartDate": "2023-04-01", "marketRank": 102, "QtyDelivered": 0, "QtyRemain": 0, "QtyOnHand": 12, }, { "EndDate": "1900-01-01", "NewPrice": 31.4500, "StartDate": "2023-08-01", "marketRank": 111, "QtyDelivered": 0, "QtyRemain": 0, "QtyOnHand": 4, } ]

Current dataweave: -for startdate and enddate i'm update the format from yyyy-mm-ddT00:00:00 to yyyy-mm-dd. i just can't figure out what to do with "NewPrice"

%dw 2.0
output application/json

---
payload[0]

map((item, index) -> item 
update {case date at .StartDate  ->  date as Date}
update {case date at .EndDate  ->  date as Date}
)

Solution

  • You can not format a number with a number of decimals positions or any kind of format. Numbers don't have formats in DataWeave nor in JSON. They will have only significant digits. You can format the number of decimal positions if you are willing to convert the number to a String.

    Example of how to convert a Number to a String with format:

    135.3000 as String {format:"0.00"}
    

    Output:

    "135.30"
    

    I advise against doing it if you are going to be using those values for calculations. In general JSON documents are not meant to be formatted.