JuiCe
JuiCe

Reputation: 4201

What does %df represent when formatting a string?

I am trying to create a VB.net equivalent to an existing Java function. Within this function, a String is formatted to have a certain amount of decimals and digits, but I cannot figure out what the function is doing exactly.

The for loop populates an ArrayList with numerical values between a given min/max. Depending on the parameter dec2disp, the numerical values are adjusted to show a certain number of decimal digits.

The Java code is as follows:

    for( double d = min; d <= max; d+=inc ) {
        if((m_flags&dispZeroAsOff)!=0 && d==0){
            vList.add(m_offString);
        }else{
            String fmt=String.format("%%.%df", dec2disp);
            String temp = String.format(fmt, d);
            vList.add(temp + unit);
        }
    }

The line that confuses me is String.format("%%.%df", dec2disp);. I have found explanations for %d and %f, but cannot find anything anywhere about %df.

Upvotes: 1

Views: 2270

Answers (3)

daved321
daved321

Reputation: 26

String fmt=String.format("%%.%df", dec2disp);

This line generates a valid format specifier for the number of decimal places requested by dec2disp (which replaces the "%d" in the "%%.%df" resulting in something like fmt="%.2f")


String temp = String.format(fmt, d);

This uses the format specifier stored in fmt to display the value of d with the correct number of decimal places.

Upvotes: 1

Natix
Natix

Reputation: 14257

%d is the only part of the string pattern that is relevant to formatting. The f is just a character literal, the dot as well and the double percent sign is also a literal, but escaped.

In the end, this pattern does not actually format decimal numbers at all. Here is a simple test to prove it:

for (int i = 0; i < 10; i++) {
    System.out.println(String.format("%%.%df", i));
}

Output:

%.0f
%.1f
%.2f
%.3f
%.4f
%.5f
%.6f
%.7f
%.8f
%.9f

If i was a float or a double, it would throw an IllegalFormatConversionException, because %d can only be applied on whole numbers.

Upvotes: 0

sjain
sjain

Reputation: 23354

I have found explanations for %d and %f, but cannot find anything anywhere about %df.

It stands for DecimalFormat.

java.lang.Object

   ↳    java.text.Format

       ↳    java.text.NumberFormat

           ↳    java.text.DecimalFormat

A concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, or Indic digits. It also supports different flavors of numbers, including integers ("123"), fixed-point numbers ("123.4"), scientific notation ("1.23E4"), percentages ("12%"), and currency amounts ("$123"). All of these flavors can be easily localized.

Reference - DecimalFormat.

NOTE:

If you are interested in different string formatters then follow the final class Formatter.

java.lang.Object
   ↳    java.util.Formatter

Upvotes: 2

Related Questions