What means %10.7f?

Discussion in 'Java' started by www, Apr 3, 2007.

  1. www

    www Guest

    Hi,

    My code has:

    System.out.printf("%10.7f", aDouble);

    My intention is to print out the value of a double variable following
    the rule: a)the maximum space is 10; b)have 7 digits after the decimal
    point.

    But there is a bug here, I found. If aDouble < 100, it is fine; If
    aDouble >= 100, it will take 11 spaces, instead of 10 spaces. So I have
    to manually change 10.7f to 10.6f. What a pain!

    Could you help me out? Thank you.
     
    www, Apr 3, 2007
    #1
    1. Advertising

  2. www

    Kai Schwebke Guest

    www schrieb:
    > But there is a bug here, I found. If aDouble < 100, it is fine; If
    > aDouble >= 100, it will take 11 spaces, instead of 10 spaces. So I have
    > to manually change 10.7f to 10.6f. What a pain!


    Man printf(3) states:
    The field width: ... In no case does a non-existent or small field
    width cause truncation of a field; ...

    It's not a bug -- it's specified behaviour since over 30 years!
    ;-)

    You may use:
    String s = String.format("%10.7f", aDouble).substring(0, 10);
    System.out.print(s);


    Kai
     
    Kai Schwebke, Apr 3, 2007
    #2
    1. Advertising

  3. Hallo www,

    Op dinsdag 03 april 2007 schreef www aan All:

    ww> My intention is to print out the value of a double variable
    ww> following the rule: a)the maximum space is 10; b)have 7
    ww> digits after the decimal point.
    ww> But there is a bug here, I found. If aDouble < 100, it is fine;
    ww> If aDouble >= 100, it will take 11 spaces, instead of 10 spaces.
    ww> So I have to manually change 10.7f to 10.6f. What a pain!

    The bug is in your thinking imho. If you want 7 decimal places, a
    number >= 100 will never fit in 10 positions. Java obviously finds
    it more important to honour the number of decimal places you asked
    for than the number of positions you asked for. So it uses one
    more postion to give you all the information you want.

    Groeten,

    Hans.

    jdh punt beekhuizen bij duinheks punt xs4all punt nl
     
    Johannes Beekhuizen, Apr 3, 2007
    #3
  4. www

    Chris Smith Guest

    Kai Schwebke <> wrote:
    > You may use:
    > String s = String.format("%10.7f", aDouble).substring(0, 10);
    > System.out.print(s);


    Of course, this will provide very bizarre results for values greater
    than 9999999999. It's not obvious how the original problem should deal
    with such a thing, if it's an issue.

    --
    Chris Smith
     
    Chris Smith, Apr 3, 2007
    #4
  5. www

    www Guest

    Thank you all. May I follow up?

    I want to print the double variable in 10 spaces, fixed! I want to have
    as many digits as possible after decimal point within the fixed 10
    spaces. How should I set it up?

    Thank you.
     
    www, Apr 3, 2007
    #5
  6. www

    Ian Wilson Guest

    www wrote:
    > Thank you all. May I follow up?
    >
    > I want to print the double variable in 10 spaces, fixed! I want to have
    > as many digits as possible after decimal point within the fixed 10
    > spaces. How should I set it up?


    Maybe something like ...

    System.out.print(fixedWidth(aDouble));

    ...

    public String fixedWidth(Double d) {
    if (d > 9999999998.9) {
    return "**********";
    } else {
    return String.format("%10.7f", aDouble).substring(0, 10);
    }
    }

    But I haven't thought through the constant very carefully :)

    You also need to think about negative values and nulls.
     
    Ian Wilson, Apr 3, 2007
    #6
  7. www

    Chris Smith Guest

    www <> wrote:
    > I want to print the double variable in 10 spaces, fixed! I want to have
    > as many digits as possible after decimal point within the fixed 10
    > spaces. How should I set it up?


    Depends on what you mean by "as many as possible". As many as possible
    would look something like this:

    public String formatToSpaces(double in)
    {
    String s = new BigDecimal(in).toString();
    int i = s.indexOf('.');

    if (i >= 10 || (i == -1 && s.length() > 10)
    {
    throw new IllegalArgumentException("too large");
    }

    if (s.length() > 10) return s.substring(0, 10);
    else return s;
    }

    If you aren't concerned with getting digits that are probably just an
    artifact of rounding error anyway, then replace the first line of the
    code with "String s = String.valueOf(in);"

    --
    Chris Smith
     
    Chris Smith, Apr 3, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rookie
    Replies:
    4
    Views:
    14,768
    Chris Langsenkamp
    Aug 20, 2003
  2. clintonG

    Re: What means Protected WithEvents ?

    clintonG, Jul 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    445
    clintonG
    Jul 5, 2003
  3. news.microsoft.com
    Replies:
    1
    Views:
    1,239
    Justin Martin
    Aug 10, 2003
  4. Andreas Klemt
    Replies:
    2
    Views:
    348
  5. ronaldlee
    Replies:
    3
    Views:
    3,575
    Hans Kesting
    Dec 16, 2004
Loading...

Share This Page