Loading

How To Remove Trailing Zeros In Numbers using Data Weave

Дата публикации: Feb 26, 2025
Задача

GOAL

The article explains how to remove trailing zeros in numbers which are being processed by DataWeave. So the number which looks like 10.1650000000000000 will look like 10.165
Действия
To achieve the above goal the following Data Weave function may be used:
%function removeTrailingZeros (numberWithTrailingZeros) (
	numberWithTrailingZeros as :string {format: "#.#############################################"} as :number
)
Please refer to the attached project which demonstrates how to achieve that.
Note, If you need more or less decimal digits, change the function respectively.

The input payload for the project looks like:
{
	"TrailingZerosNumber": 10.165000000000000000000,
	"TrailingZerosNegativeNumber": -10.1650000000000000000000,
	"NoTrailingZerosNumber": 10.165,
	"NoTrailingZerosNegativeNumber": -10.165
}
So it represents all possible use cases in removing trailing zeros.

To test the project execute the following command from the command like:
curl -i -H "content-type: application/json" -X POST -d '{ "TrailingZerosNumber": 10.165000000000000000000, "TrailingZerosNegativeNumber": -10.1650000000000000000000, "NoTrailingZerosNumber": 10.165, "NoTrailingZerosNegativeNumber": -10.165 }' http://localhost:8081/trailing
The output looks similar to the below:
HTTP/1.1 200
Transfer-Encoding: chunked
MULE_ENCODING: UTF-8
Content-Type: application/json
Date: Fri, 13 Apr 2018 03:51:41 GMT

{
  "ProcessedTrailingZerosNumber": 10.165,
  "ProcessedTrailingZerosNegativeNumber": -10.165,
  "ProcessedNoTrailingZerosNumber": 10.165,
  "ProcessedNoTrailingZerosNegativeNumber": -10.165
}
To ensure the solution works for a particular case, it is recommended to give a try to different trailing zero numbers before continuing. 

Disclaimer: this is provided as a reference for your own usage and it's not part of the official Mule product so its use will be considered as a custom implementation made by the customer.
 
Дополнительные ресурсы
dw-remove-trailing-zeros.zip
Номер статьи базы знаний

001114983

 
Загрузка
Salesforce Help | Article