Groovy way to format BigDecimal with thousands separator

View: New views
8 Messages — Rating Filter:   Alert me  

Groovy way to format BigDecimal with thousands separator

by Michael Potter-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Groovy Crew,

What are my options to format a BigDecimal with commas?

I see code using DecimalFormat( "#,###,###,##0.00" ), but
DecimalFormat requires the BigDecimal be converted to double and I do
not want to take the chance of changing the original number.

I also see BigDecimalLayoutForm(), but I can not find a decent example
to work from and the documentation from Sun is no help.

I figure there is some Groovy one liner that will do this for me so I
asking here.

Thanks,
--
Michael Potter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Groovy way to format BigDecimal with thousands separator

by Tim Yates :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This does it I believe:

def a = 12345678902345677.45
println String.format( '%,f', a )

That's a core Java method though, not Groovy

Tim

On Tue, Oct 27, 2009 at 2:15 PM, Michael Potter <michael@...> wrote:
Groovy Crew,

What are my options to format a BigDecimal with commas?

I see code using DecimalFormat( "#,###,###,##0.00" ), but
DecimalFormat requires the BigDecimal be converted to double and I do
not want to take the chance of changing the original number.

I also see BigDecimalLayoutForm(), but I can not find a decent example
to work from and the documentation from Sun is no help.

I figure there is some Groovy one liner that will do this for me so I
asking here.

Thanks,
--
Michael Potter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Groovy way to format BigDecimal with thousands separator

by Jochen Theodorou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael Potter schrieb:
> Groovy Crew,
>
> What are my options to format a BigDecimal with commas?
>
> I see code using DecimalFormat( "#,###,###,##0.00" ), but
> DecimalFormat requires the BigDecimal be converted to double and I do
> not want to take the chance of changing the original number.

DecimalFormat has also an Object taking format method you can use here

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Groovy way to format BigDecimal with thousands separator

by Michael Potter-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Oct 27, 2009 at 10:46 AM, Jochen Theodorou <blackdrag@...> wrote:

> Michael Potter schrieb:
>>
>> Groovy Crew,
>>
>> What are my options to format a BigDecimal with commas?
>>
>> I see code using DecimalFormat( "#,###,###,##0.00" ), but
>> DecimalFormat requires the BigDecimal be converted to double and I do
>> not want to take the chance of changing the original number.
>
> DecimalFormat has also an Object taking format method you can use here
>
> bye blackdrag
>
> --
> Jochen "blackdrag" Theodorou
> The Groovy Project Tech Lead (http://groovy.codehaus.org)
> http://blackdragsview.blogspot.com/
>


Jochen,

Are you saying that the DecimalFormat(Object) constructor will take a
BigDecimal?

--
Michael Potter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Groovy way to format BigDecimal with thousands separator

by Russel Winder-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2009-10-27 at 14:31 +0000, Tim Yates wrote:
> This does it I believe:
>
> def a = 12345678902345677.45
> println String.format( '%,f', a )
>
>
> That's a core Java method though, not Groovy
>
If it's in the Java Platform then its Groovy :-)
--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                            xmpp: russel@...
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: sip:russel.winder@...
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder


signature.asc (204 bytes) Download Attachment

Re: Groovy way to format BigDecimal with thousands separator

by Tim Yates :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yeah, I know...  That was in response to them asking for a "Groovy one-liner"

;-)

On Tue, Oct 27, 2009 at 3:48 PM, Russel Winder <russel.winder@...> wrote:
On Tue, 2009-10-27 at 14:31 +0000, Tim Yates wrote:
> This does it I believe:
>
> def a = 12345678902345677.45
> println String.format( '%,f', a )
>
>
> That's a core Java method though, not Groovy
>
If it's in the Java Platform then its Groovy :-)
--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                           xmpp: russel@...
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: sip%3Arussel.winder@...
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder


Re: Groovy way to format BigDecimal with thousands separator

by Russel Winder-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael,

In case it isn't clear from the exchange, java.text.DecimalFormatter is
almost certainly not what you want, java.util.Formatter is much more
likely to be a better general tool for rendering values.  Also cf.
String.format

On Tue, 2009-10-27 at 10:15 -0400, Michael Potter wrote:

> Groovy Crew,
>
> What are my options to format a BigDecimal with commas?
>
> I see code using DecimalFormat( "#,###,###,##0.00" ), but
> DecimalFormat requires the BigDecimal be converted to double and I do
> not want to take the chance of changing the original number.
>
> I also see BigDecimalLayoutForm(), but I can not find a decent example
> to work from and the documentation from Sun is no help.
>
> I figure there is some Groovy one liner that will do this for me so I
> asking here.
>
> Thanks,
--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                            xmpp: russel@...
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: sip:russel.winder@...
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder


signature.asc (204 bytes) Download Attachment

Re: Groovy way to format BigDecimal with thousands separator

by Russel Winder-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2009-10-27 at 15:51 +0000, Tim Yates wrote:
> Yeah, I know...  That was in response to them asking for a "Groovy
> one-liner"
>
> ;-)

:-)

>        
>         > def a = 12345678902345677.45
>         > println String.format( '%,f', a )

>        
The important point here is that java.util.Formatter knows that
BigDecimals are valid % slot -- e, E, f, F, g or G I would guess -- and
is a much better way of formatting values than using
java.text.DecimalFormatter.
>
--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                            xmpp: russel@...
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: sip:russel.winder@...
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder


signature.asc (204 bytes) Download Attachment