John R.
John R.

Reputation: 420

The width of a double variable

I am trying to write some text to a file using PrintWriter and I do not understand what "7" means in the next example:

double d = 12.11211;
out.format(“%7.3f”, d);

The source from where I am inspiring says that 7 means:

The 7.3 before the f denotes the width (7) and precision (3) to output d in.

The problem is that I cannot understand what that width really means. I mean, even though my variable would have more than 7 digits, it is not going to format anything.

Upvotes: 3

Views: 2015

Answers (5)

pavi2410
pavi2410

Reputation: 1285

By width, it means the string will always fill the rest of the places if the number of digits/characters of the value is less than the given number.

double d = 12.11211;
String.format(“%7.3f”, d);


> 12.112
 ^^^^^^^
 1234567

Upvotes: 2

forpas
forpas

Reputation: 164099

Think of it as the total length of the string produced by the applied format.
If you take in account that there will be exactly 3 chars after the decimal point and 1 char for the decimal point itself, then it leaves 3 chars for the integer part.
Because your number has only 2 digits in the integer part the final result will be padded at the left with a white space:

double d = 12.11211;
out.format("%7.3f", d);

will create:

" 12.112"

with

out.format("%8.3f", d);

you get

"  12.112"

and so on.

The minimum length of the formatted string is in your case:

6 = 2 chars for the integer part + 1 char for the decimal point + 3 chars for the digits after the decimal point

so even if you set:

out.format("%5.3f", d);

this will not truncate the result, it will be:

"12.112"

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691755

Your source of inspiration should be the javadoc. It says:

The width is the minimum number of characters to be written to the output. If the length of the converted value is less than the width then the output will be padded by ' ' ('\u0020') until the total number of characters equals the width. The padding is on the left by default.[...]

For the floating-point conversions 'a', 'A', 'e', 'E', and 'f' the precision is the number of digits after the radix point.

A simple experiment shows it:

    System.out.printf("%7.3f%n", 1.3);
    System.out.printf("%7.3f%n", 12.3);
    System.out.printf("%7.3f%n", 12.34);
    System.out.printf("%7.3f%n", 12.345);
    System.out.printf("%7.3f%n", 12.3456);
    System.out.printf("%7.3f%n", 12.34567);
    System.out.printf("%7.3f%n", 12.34567);
    System.out.printf("%7.3f%n", 123.34567);
    System.out.printf("%7.3f%n", 1234.34567);
    System.out.printf("%7.3f%n", 12345.34567);

prints:

  1,300
 12,300
 12,340
 12,345
 12,346
 12,346
 12,346
123,346
1234,346
12345,346

Upvotes: 3

avigloz
avigloz

Reputation: 46

According to this:

The field width in a formatting operator is a nonnegative integer that specifies the number of digits or characters in the output when formatting input values. For example, in the operator %7.3f, the field width is 7.

By default, the output text is padded with space characters when the field width is greater than the number of characters.

The 7 adds padding () in case the output is smaller than 7 characters. This way the output is always at least 7 characters, with 3 floating decimals.

This documentation gives a very detailed explanation for formatting text in general, most of which is applicable cross-language.

Upvotes: 1

Hamid Ghasemi
Hamid Ghasemi

Reputation: 892

For simple text arguments, you can think of the width and precision as a minimum and maximum number of characters to be output. For floating-point numeric types, the precision changes meaning slightly and controls the number of digits displayed after the decimal point.

watch this example:

System.out.printf("String is '%5s'\n", "A");
// String is '    A'
System.out.printf("String is '%.5s'\n", "Happy Birthday!");
// String is 'Happy'

for more information look at this link

Upvotes: 3

Related Questions