Home > Software engineering >  improve script to format string based on char position
improve script to format string based on char position

Time:04-03

I have an array of numbers like:

["5119229", "757218"]

and I must output them with a dot after the second position:

["51.19229", "75.7218"]

I achieved it by this script:

payload map ($[0 to 1]    "."    $[2 to -1])

is there a better way to do this using dataweave 2 (and its libraries?)

CodePudding user response:

You have an array of strings, not numbers.

This is cleaner though not more efficient; there are other questions like: are the string always the same length? If one is too short, what should we do? Etc.

%dw 2.0
output application/json

fun formatItem(itm: String) = "$(itm[0 to 1]).$(itm[2 to -1])"
---
payload map formatItem($)

CodePudding user response:

Probably not. There is no currently support for fixed point numbers in DataWeave and your numbers are not fixed length.

  • Related