[
https://issues.apache.org/jira/browse/GROOVY-12147?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King updated GROOVY-12147:
-------------------------------
Description:
h2. Summary
EDIT: updated to match what it would be after PR#2699
Groovy can already format and parse numbers, but only in a locale-invariant
way. {{String.format(locale, ...)}} / {{sprintf}} cover locale-aware
_grouped/decimal_ output, and {{"1234.50".toBigDecimal()}} covers _canonical_
parsing — but there is no idiomatic way to:
* format a number as *locale currency* ({{"€1.234,50"}}) or *locale percent*
({{"94,5 %"}}), or
* parse a *locale-formatted* number, currency or percent string back to a
{{Number}}.
These are exactly the cases {{java.util.Formatter}} ({{%f}}, {{%%}})
structurally cannot express (no currency conversion; {{%%}} is a literal sign,
so no ÷100 scaling and wrong symbol placement in locales such as Turkish
{{%94,5}} or German {{94,5 %}}). This issue adds a small set of GDK extension
methods that wrap {{java.text.NumberFormat}} to close that gap.
h2. Motivation
* {{StringGroovyMethods}} conversions ({{toInteger}}, {{toDouble}},
{{toBigDecimal}}, {{isNumber}}, …) are locale-invariant by construction ({{new
BigDecimal(str)}} / {{Double.valueOf(str)}}) — they reject grouping separators,
foreign decimal marks, currency symbols and percent signs.
* {{sprintf}}/{{Formatter}} handles grouped/decimal numbers and localized
{{%t}} dates, so no new date/time or {{sprintf}} surface is needed.
* The only genuinely missing capability is currency/percent formatting and
locale-aware parsing — a handful of thin {{NumberFormat}} wrappers.
h2. Proposed API
*Formatting* — extension methods on {{Number}} (in {{DefaultGroovyMethods}}):
{code:java}
public static String toCurrencyString(Number self)
public static String toCurrencyString(Number self, Locale locale)
public static String toPercentString(Number self)
public static String toPercentString(Number self, Locale locale)
{code}
*Parsing* — extension methods on {{CharSequence}} (in {{StringGroovyMethods}},
alongside {{toBigDecimal}}):
{code:java}
public static Number toNumber(CharSequence self, Locale locale)
// public static Number toCurrencyNumber(CharSequence self) // EDIT: removed
public static Number toCurrencyNumber(CharSequence self, Locale locale)
// public static Number toPercentNumber(CharSequence self) // EDIT: removed
public static Number toPercentNumber(CharSequence self, Locale locale)
{code}
h2. Example
{code:groovy}
import java.util.Locale
def de = Locale.GERMANY
assert 1234.5.toCurrencyString(de) == '1.234,50 €'
assert 0.945.toPercentString(de) == '94,5 %'
assert '1.234,50 €'.toCurrencyNumber(de) == 1234.50G // BigDecimal
assert '94,5 %'.toPercentNumber(de) == 0.945
assert '1.234,50'.toNumber(de) == 1234.5
{code}
h2. Tests
* Round-trip format→parse for representative locales (US, Germany, France,
Turkey — Turkey to cover leading {{%}} placement).
* Currency parse returns {{BigDecimal}}.
* Percent parse applies ÷100 scaling.
* Malformed input throws {{NumberFormatException}}.
was:
h2. Summary
[EDIT: updated to match what it would be after PR#2699]
Groovy can already format and parse numbers, but only in a locale-invariant
way. {{String.format(locale, ...)}} / {{sprintf}} cover locale-aware
_grouped/decimal_ output, and {{"1234.50".toBigDecimal()}} covers _canonical_
parsing — but there is no idiomatic way to:
* format a number as *locale currency* ({{"€1.234,50"}}) or *locale percent*
({{"94,5 %"}}), or
* parse a *locale-formatted* number, currency or percent string back to a
{{Number}}.
These are exactly the cases {{java.util.Formatter}} ({{%f}}, {{%%}})
structurally cannot express (no currency conversion; {{%%}} is a literal sign,
so no ÷100 scaling and wrong symbol placement in locales such as Turkish
{{%94,5}} or German {{94,5 %}}). This issue adds a small set of GDK extension
methods that wrap {{java.text.NumberFormat}} to close that gap.
h2. Motivation
* {{StringGroovyMethods}} conversions ({{toInteger}}, {{toDouble}},
{{toBigDecimal}}, {{isNumber}}, …) are locale-invariant by construction ({{new
BigDecimal(str)}} / {{Double.valueOf(str)}}) — they reject grouping separators,
foreign decimal marks, currency symbols and percent signs.
* {{sprintf}}/{{Formatter}} handles grouped/decimal numbers and localized
{{%t}} dates, so no new date/time or {{sprintf}} surface is needed.
* The only genuinely missing capability is currency/percent formatting and
locale-aware parsing — a handful of thin {{NumberFormat}} wrappers.
h2. Proposed API
*Formatting* — extension methods on {{Number}} (in {{DefaultGroovyMethods}}):
{code:java}
public static String toCurrencyString(Number self)
public static String toCurrencyString(Number self, Locale locale)
public static String toPercentString(Number self)
public static String toPercentString(Number self, Locale locale)
{code}
*Parsing* — extension methods on {{CharSequence}} (in {{StringGroovyMethods}},
alongside {{toBigDecimal}}):
{code:java}
public static Number toNumber(CharSequence self, Locale locale)
// public static Number toCurrencyNumber(CharSequence self) // EDIT: removed
public static Number toCurrencyNumber(CharSequence self, Locale locale)
// public static Number toPercentNumber(CharSequence self) // EDIT: removed
public static Number toPercentNumber(CharSequence self, Locale locale)
{code}
h2. Example
{code:groovy}
import java.util.Locale
def de = Locale.GERMANY
assert 1234.5.toCurrencyString(de) == '1.234,50 €'
assert 0.945.toPercentString(de) == '94,5 %'
assert '1.234,50 €'.toCurrencyNumber(de) == 1234.50G // BigDecimal
assert '94,5 %'.toPercentNumber(de) == 0.945
assert '1.234,50'.toNumber(de) == 1234.5
{code}
h2. Tests
* Round-trip format→parse for representative locales (US, Germany, France,
Turkey — Turkey to cover leading {{%}} placement).
* Currency parse returns {{BigDecimal}}.
* Percent parse applies ÷100 scaling.
* Malformed input throws {{NumberFormatException}}.
> Add locale-aware number, currency and percent formatting/parsing to the GDK
> ---------------------------------------------------------------------------
>
> Key: GROOVY-12147
> URL: https://issues.apache.org/jira/browse/GROOVY-12147
> Project: Groovy
> Issue Type: Improvement
> Components: groovy-jdk
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
> Fix For: 6.0.0-beta-1
>
>
> h2. Summary
> EDIT: updated to match what it would be after PR#2699
> Groovy can already format and parse numbers, but only in a locale-invariant
> way. {{String.format(locale, ...)}} / {{sprintf}} cover locale-aware
> _grouped/decimal_ output, and {{"1234.50".toBigDecimal()}} covers _canonical_
> parsing — but there is no idiomatic way to:
> * format a number as *locale currency* ({{"€1.234,50"}}) or *locale percent*
> ({{"94,5 %"}}), or
> * parse a *locale-formatted* number, currency or percent string back to a
> {{Number}}.
> These are exactly the cases {{java.util.Formatter}} ({{%f}}, {{%%}})
> structurally cannot express (no currency conversion; {{%%}} is a literal
> sign, so no ÷100 scaling and wrong symbol placement in locales such as
> Turkish {{%94,5}} or German {{94,5 %}}). This issue adds a small set of GDK
> extension methods that wrap {{java.text.NumberFormat}} to close that gap.
> h2. Motivation
> * {{StringGroovyMethods}} conversions ({{toInteger}}, {{toDouble}},
> {{toBigDecimal}}, {{isNumber}}, …) are locale-invariant by construction
> ({{new BigDecimal(str)}} / {{Double.valueOf(str)}}) — they reject grouping
> separators, foreign decimal marks, currency symbols and percent signs.
> * {{sprintf}}/{{Formatter}} handles grouped/decimal numbers and localized
> {{%t}} dates, so no new date/time or {{sprintf}} surface is needed.
> * The only genuinely missing capability is currency/percent formatting and
> locale-aware parsing — a handful of thin {{NumberFormat}} wrappers.
> h2. Proposed API
> *Formatting* — extension methods on {{Number}} (in {{DefaultGroovyMethods}}):
> {code:java}
> public static String toCurrencyString(Number self)
> public static String toCurrencyString(Number self, Locale locale)
> public static String toPercentString(Number self)
> public static String toPercentString(Number self, Locale locale)
> {code}
> *Parsing* — extension methods on {{CharSequence}} (in
> {{StringGroovyMethods}}, alongside {{toBigDecimal}}):
> {code:java}
> public static Number toNumber(CharSequence self, Locale locale)
> // public static Number toCurrencyNumber(CharSequence self) // EDIT: removed
> public static Number toCurrencyNumber(CharSequence self, Locale locale)
> // public static Number toPercentNumber(CharSequence self) // EDIT: removed
> public static Number toPercentNumber(CharSequence self, Locale locale)
> {code}
> h2. Example
> {code:groovy}
> import java.util.Locale
> def de = Locale.GERMANY
> assert 1234.5.toCurrencyString(de) == '1.234,50 €'
> assert 0.945.toPercentString(de) == '94,5 %'
> assert '1.234,50 €'.toCurrencyNumber(de) == 1234.50G // BigDecimal
> assert '94,5 %'.toPercentNumber(de) == 0.945
> assert '1.234,50'.toNumber(de) == 1234.5
> {code}
> h2. Tests
> * Round-trip format→parse for representative locales (US, Germany, France,
> Turkey — Turkey to cover leading {{%}} placement).
> * Currency parse returns {{BigDecimal}}.
> * Percent parse applies ÷100 scaling.
> * Malformed input throws {{NumberFormatException}}.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)