[
https://issues.apache.org/jira/browse/GROOVY-11569?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17927588#comment-17927588
]
ASF GitHub Bot commented on GROOVY-11569:
-----------------------------------------
eric-milles commented on PR #2152:
URL: https://github.com/apache/groovy/pull/2152#issuecomment-2661840048
I understand the use case. But the existing `sprintf` extensions don't work
this way. Adding a variant with much different usage is a problem IMO. We're
only saving a few chars versus:
```groovy
def nums = (2..7).collect { 1 / it }
def strs = nums.collect { sprintf('%.2f', it) }
assert strs == ['0.50', '0.33', '0.25', '0.20', '0.17', '0.14']
```
I was trying to think if `import static java.lang.String.format as sprintf`
could replace the existing extension methods. However, I see that they were
added in 1.5.0 so I'm not sure if dropping them is a good plan.
> Create a sprintf shortcut for 1-arg usage
> -----------------------------------------
>
> Key: GROOVY-11569
> URL: https://issues.apache.org/jira/browse/GROOVY-11569
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> Currently we support:
> {code:groovy}
> var x = 1/3
> assert sprintf('%5.3f', x) == '0.333'
> {code}
> Or you can use:
> {code:groovy}
> assert String.format('%5.3f', x) == '0.333'
> {code}
> I propose we also support:
> {code:groovy}
> assert x.sprintf('%5.3f') == '0.333'
> assert [x, x, x]*.sprintf('%5.3f') == ['0.333', '0.333', '0.333']
> {code}
> It is intended to be a shortcut only for format strings containing a single
> format specifier (or at least referencing a single argument).
--
This message was sent by Atlassian Jira
(v8.20.10#820010)