Results 1 to 11 of 11
  1. #1
    wmd
    wmd is offline Member
    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0

    Default Alternative to DecimalFormat class?

    Are there any other classes I can use to comma separate format an integer, e.g. 100000000 becomes 100,000,000? The proviso is that the object must not create any garbage. The DecimalFormat class creates garbage, which renders it useless for my requirements.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Alternative to DecimalFormat class?

    Quote Originally Posted by wmd View Post
    The DecimalFormat class creates garbage
    Huh?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    wmd
    wmd is offline Member
    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0

    Default Re: Alternative to DecimalFormat class?

    Yep, every time you call a DecimalFormat object, it creates heap allocations which results in garbage collector invocations. Well, it does on Android anyway, which is what I'm developing for.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Alternative to DecimalFormat class?

    Quote Originally Posted by wmd View Post
    Android ... is what I'm developing for.
    Moved from New to Java..

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Alternative to DecimalFormat class?

    Is the Android garbage collector really so inefficient that creating and discarding objects results in a performance hit? I find that rather surprising.

    You could probably roll your own class to parse an int and output the required String, Using a single instance of a StringBuilder internally could mean that the only new object created is the returned String.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    wmd
    wmd is offline Member
    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0

    Default Re: Alternative to DecimalFormat class?

    Calling an existing DecimalFormat object in a loop creates enough garbage to cause a collection every 40 seconds or so. I'm working on a game and this is causing stuttering in my app when the GC kicks in. I also tried String.format but this also creates garbage. I just wanted to double check that there wasn't already a Java class I could use before I spent time on my own solution.
    Last edited by wmd; 09-10-2012 at 11:53 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Alternative to DecimalFormat class?

    How many times are you formatting a number then?
    Because DecimalFormat doesn't produce that much data on a single run.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    wmd
    wmd is offline Member
    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0

    Default Re: Alternative to DecimalFormat class?

    It depends on what the player is doing. It's the player score/high score that I'm formatting.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Alternative to DecimalFormat class?

    I suppose if the score racks up rapidly (I'm thinking daka-daka-daka) then possibly.
    Just check you are only formatting when you have to (on score change I suppose).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    wmd
    wmd is offline Member
    Join Date
    Jun 2012
    Posts
    15
    Rep Power
    0

    Default Re: Alternative to DecimalFormat class?

    Yeah, I am only updating the scores on score change. Enemies can be destroyed in rapid succession, and there are also other elements which can cause the score to increase quickly. I'm surprised that it seems so elusive as to how to convert an int to a String without creating data on the heap. I've tried all sorts but everything produces heap data. Even reusing a StringBuilder creates heap data as the int has to be converted to a String before it can be appended to a StringBuilder.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Alternative to DecimalFormat class?

    If the display needs a String then you have to create something.
    That something needs iterations to format it (for each comma).
    How else are you going to do that without creating objects (Strings in this case)?

    Without really knowing the ins and outs of the problem, though, it's going to be hard to suggest anything.
    You could, for example, hold the score as units, thousands and millions, each populating a comma separated field on the screen (a bit like the old pinball machines).
    Or look at a way of batching score updates...that might be preferable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Dollar sign and DecimalFormat
    By javauserjava in forum New To Java
    Replies: 7
    Last Post: 04-05-2011, 11:57 PM
  2. Advice needed on DecimalFormat class
    By israelyan in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 01:34 AM
  3. Error involving DecimalFormat
    By kamikaze in forum New To Java
    Replies: 4
    Last Post: 10-18-2010, 02:04 AM
  4. locale and decimalFormat confusion
    By bz3x in forum New To Java
    Replies: 2
    Last Post: 05-23-2010, 12:22 AM
  5. DecimalFormat class
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 12-30-2007, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •