Show List

String.format

In Java, you can format different data types using String.format by specifying format specifiers for each type. Format specifiers control how the values are presented in the resulting formatted string. Here are some common data types and their format specifiers:

  1. Integers (int, long):

    • %d: Decimal integer.
    • %o: Octal integer.
    • %x or %X: Hexadecimal integer (lowercase or uppercase).

    int intValue = 42; long longValue = 1234567890L; String formattedInt = String.format("Decimal: %d", intValue); String formattedOctal = String.format("Octal: %o", intValue); String formattedHex = String.format("Hexadecimal: %X", intValue); String formattedLong = String.format("Long: %d", longValue);
  2. Floating-Point Numbers (float, double):

    • %f: Floating-point number.
    • %e or %E: Scientific notation (lowercase or uppercase).
    • %g or %G: General format (chooses %f or %e based on precision).

    float floatValue = 3.1415f; double doubleValue = 123.456789; String formattedFloat = String.format("Floating-Point: %f", floatValue); String formattedScientific = String.format("Scientific: %e", doubleValue); String formattedGeneral = String.format("General: %g", doubleValue);
  3. Strings:

    • %s: String.
    • %10s: Right-justified string in a field of at least 10 characters (adjust the number as needed).

    String stringValue = "Hello, World!"; String formattedString = String.format("String: %s", stringValue); String formattedRightJustified = String.format("Right-Justified: %10s", stringValue);
  4. Dates (java.util.Date, java.time.LocalDate, etc.):

    • %t: Date/time.

    import java.util.Date; Date currentDate = new Date(); String formattedDate = String.format("Date: %tF", currentDate);

Padding using String.format

you can also pad different data types using String.format by specifying width and alignment options within the format specifiers. Here are examples for different data types:

  1. Pad Integers (int, long):

    You can specify the minimum width and pad with leading zeros or spaces using %d for integers.

    • To pad with leading zeros:


      int intValue = 42; String formattedInt = String.format("Padded Integer: %05d", intValue); // At least 5 characters wide with leading zeros
    • To pad with spaces (right-justified):


      int intValue = 42; String formattedInt = String.format("Padded Integer: %5d", intValue); // At least 5 characters wide with spaces
  2. Pad Floating-Point Numbers (float, double):

    You can specify the minimum width and pad with leading zeros or spaces using %f for floating-point numbers.

    • To pad with leading zeros:


      double doubleValue = 3.1415; String formattedDouble = String.format("Padded Double: %07.2f", doubleValue); // At least 7 characters wide with leading zeros and 2 decimal places
    • To pad with spaces (right-justified):


      double doubleValue = 3.1415; String formattedDouble = String.format("Padded Double: %10.2f", doubleValue); // At least 10 characters wide with spaces and 2 decimal places
  3. Pad Strings:

    You can specify the minimum width and pad with spaces using %s for strings.

    • To right-pad with spaces:


      String stringValue = "Hello"; String formattedString = String.format("Padded String: %-10s", stringValue); // At least 10 characters wide with left-justified text
    • To left-pad with spaces:


      String stringValue = "Hello"; String formattedString = String.format("Padded String: %10s", stringValue); // At least 10 characters wide with right-justified text



    Leave a Comment


  • captcha text