改进基于char位置格式字符串的脚本

发布于 2025-01-18 11:26:55 字数 268 浏览 1 评论 0原文

我有一个数字数组,例如:

["5119229", "757218"]

并且我必须在第二个位置后用点输出它们:

["51.19229", "75.7218"]

我通过此脚本实现了它:

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

是否有更好的方法使用 dataweave 2 (及其库?)

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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2025-01-25 11:26:55

您有一系列字符串,而不是数字。

这更干净,尽管效率不高。还有其他问题,例如:字符串总是相同的长度吗?如果一个人太短,我们该怎么办? ETC。

%dw 2.0
output application/json

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

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($)
放低过去 2025-01-25 11:26:55

可能不会。 DataWeave 目前不支持定点数,并且您的数字不是固定长度。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文