# What means %10.7f?

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

1. ### wwwGuest

Hi,

My code has:

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

2. ### Kai SchwebkeGuest

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

3. ### Johannes BeekhuizenGuest

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
4. ### Chris SmithGuest

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
5. ### wwwGuest

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
6. ### Ian WilsonGuest

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 ...

...

public String fixedWidth(Double d) {
if (d > 9999999998.9) {
return "**********";
} else {
}
}

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
7. ### Chris SmithGuest

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