Results 1 to 15 of 15
  1. #1
    dexdyne is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default decimal formatting strings

    I have inherited some code which has format strings for DecimalFormat

    Some of the use #0 in front of the decimal point, which confuses me somewhat.

    as far as I can see, putting 0. means "print as many digits before the decimal place as my value happens to have, unless it's<1.0, in which case print 1"

    So what possible use is adding more # characters - aren't they implied???

    TVM

    David

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: decimal formatting strings

    try to print two different format for you to see the result. One DecimalFormat is '#,##0.00' and the other one is '0,000.00'

  3. #3
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default Re: decimal formatting strings

    I cant see any difference myself. I've done a few tests and read through the api and they appear to be synonymous. This is always the fun you get when inheriting code. I wouldn't get too hung up on why the wrote it like they did. If more than one person wrote the code then that may give a reason for the difference. Sometimes people will add surplus code like that simply because they think it's clearer to read.
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: decimal formatting strings

    Java Code:
        public static void main(String[] args) {
            DecimalFormat df1 = new DecimalFormat("#,##0.00");
            DecimalFormat df2 = new DecimalFormat("0,000.00");
            double d = 12.1;
            System.out.println("df1 = " + df1.format(d));
            System.out.println("df2 = " + df2.format(d));
        }
    produces:
    df1 = 12.10
    df2 = 0,012.10

    Hardly synonymous.

  5. #5
    dexdyne is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: decimal formatting strings

    Thanks for the replies, guys.

    Biggest problem is I couldn't find a decent comprehensive spec for these strings on the web ... there are a lot of tables showing the meaning of individual characters, but little about the effect of combinations of them.

    My set-up is a bit complex, using eclipse to run on a remote server, and writing 5-liners isn't as easy as it should be!

    I was hoping to find an interactive web page, where you put in a format string and a value, but came up empty.

    ------------------------------------------

    The example is great, but much more complex than what I'm looking at

    the code includes stuff like "#0.0###"

    I'm quite clear about the stuff after the dec pt.

    What puzzled me is whether the pound at the beginning could ever DO anything.

    I did wonder overnight whether it would be replaced by a leading space, but clearly not.

    ------------------

    So pardon my ignorance, but if I want something which can handle fixed field lengths, and will barf if I try to print 10000 in a 4-position field, what are my options?

    David

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: decimal formatting strings

    I am not sure if I understand you correctly but are you trying to allow the user to input upto four(4) decimal?

    So when user
    input: 100
    output is : 100.0

    and when user
    input: 100.2369
    output is same which is: 100.2369

    You do not want to round it into something like 100.2

    Is that what you are trying to do?

    EDT:
    Have you tried to experiments on the formats? and input different value to see which one gives you the output that you want?
    Last edited by mine0926; 09-13-2011 at 11:32 AM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: decimal formatting strings

    Quote Originally Posted by dexdyne View Post
    Biggest problem is I couldn't find a decent comprehensive spec for these strings on the web ... there are a lot of tables showing the meaning of individual characters, but little about the effect of combinations of them.
    Bookmark this link; it's all in there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    dexdyne is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: decimal formatting strings

    Nope, not input, printout.

    David


    Quote Originally Posted by mine0926 View Post
    I am not sure if I understand you correctly but are you trying to allow the user to input upto four(4) decimal?

    So when user
    input: 100
    output is : 100.0

    and when user
    input: 100.2369
    output is same which is: 100.2369

    You do not want to round it into something like 100.2

    Is that what you are trying to do?

    EDT:
    Have you tried to experiments on the formats? and input different value to see which one gives you the output that you want?

  9. #9
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default Re: decimal formatting strings

    As I mentioned in my previous post, there is no difference in the scenario you're describing.

    #0.0### Appears to format (output) the same way as 0.0###

    I haven't tested parsing (input).

    Joash, the OP has already mentioned documentation containing tables showing character meanings but not found them helpful. That reasonably describes this. And he is right, it does not easily answer his question!

    There is a hint (but not a definitive answer) in that leading '#' characters don't affect anything for exponential numbers (1.234E5) but the doc is silent on whether it applies to other number formats.

    If I've missed something, feel free to quote the manual.
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: decimal formatting strings

    # is used to say "these are optional numbers".
    It is (as shown) pointless if you aren't attempting to show what separators to use (and where), or using the scientific notation stuff.
    "Digit, zero shows as absent".
    It's pretty clear.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: decimal formatting strings

    Quote Originally Posted by couling View Post
    Joash, the OP has already mentioned documentation containing tables showing character meanings but not found them helpful. That reasonably describes this. And he is right, it does not easily answer his question!

    There is a hint (but not a definitive answer) in that leading '#' characters don't affect anything for exponential numbers (1.234E5) but the doc is silent on whether it applies to other number formats.

    If I've missed something, feel free to quote the manual.
    In the section 'Special Pattern Characters', it says it all about the # character; I admit, it isn't a novel but what more is there to say about it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default Re: decimal formatting strings

    Symbol; Location; Localized?; Meaning;
    #; Number; Yes; Digit, zero shows as absent
    Quote Originally Posted by JosAH View Post
    what more is there to say about it?
    Wasn't that the point of the OP's question?

    The answer that has been provided to the OP's question by this forum is basically summed up as:
    "#" has no effect when used on its own at the start of a format (eg: "#0.00" vs just "0.00"), because the number of digits is automatically variable anyway.

    The OP's question was valid even after reading the manual sice he's seen code where (in the same code) both "#0.00" and "0.00" are used. When you see something in code like this, it can imply the person who wrote it knew something you don't. This is more than enough to suspect that "#0.00" and "0.00" could represent different decimal formats. The manual doesn't explain a difference and doesn't state they are the same (unless I've missed something). The simple inference in the manual that they are the same is not enough to answer a question such as "why does someone else think they are different" and so the manual can not be used to directly answer the OP's question.
    Last edited by couling; 09-15-2011 at 12:07 PM.
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: decimal formatting strings

    Of course it can.
    The answer to "why does someone else think they are different", from the documentation, is "they are mistaken".

  14. #14
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: decimal formatting strings

    This is more than enough to suspect that "#0.00" and "0.00" could represent different decimal formats.
    I think the only ones who will think they are different are those non-programmers OR those who misunderstood the use of "#" and "0" in formatting numbers

  15. #15
    dexdyne is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: decimal formatting strings

    "Digit, zero shows as absent".
    It's pretty clear.

    Well yeah, it is. But my "previous programmer" clearly thought otherwise, and I wasn't quite arrogant enough not to be curious.

    There was the vague possibility that leading # would turn into leading space... and that was documented somewhere I hadn't looked.

    Thanks all.

    David

Similar Threads

  1. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  2. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  3. Formatting Strings in Java
    By ladykrimson in forum New To Java
    Replies: 3
    Last Post: 11-03-2010, 08:31 AM
  4. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 07:11 PM
  5. get percentage formatting to display 2 decimal places
    By gotenks05 in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 04:57 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
  •