What means %10.7f?

W

www

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

Kai Schwebke

www said:
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
 
J

Johannes Beekhuizen

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
 
C

Chris Smith

Kai Schwebke said:
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.
 
W

www

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

Ian Wilson

www said:
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.
 
C

Chris Smith

www said:
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);"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top