Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to format a string using fixed-width fields in Kotlin

6 Answers

0 votes
/*
Common format specifiers
-----------------------------------
%-10s // Left-aligned string, width 10
%10s // Right-aligned string, width 10
%5d // Integer, width 5
%08d // Integer, width 8, padded with zeros
%12.2f // Floating-point, width 12, 2 decimal places
*/

/**
 * Formats a record using fixed-width fields.
 *
 * %-15s = left-aligned string, width 15
 * %10d  = right-aligned integer, width 10
 * %12.2f = right-aligned floating-point number,
 *           width 12, 2 decimal places
 */
fun formatRecord(name: String, age: Int, salary: Double): String {
    return String.format(
        "%-15s%10d%12.2f",
        name,
        age,
        salary
    )
}

fun main() {
    // Store the formatted string in a variable
    val result = formatRecord("Silvia", 23, 169000.0)

    println(result)
}


/*
run:

Silvia                 23   169000.00

*/

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
0 votes
fun main() {
    // Left-align "Hello" in a field 10 characters wide
    val result = String.format("%-10s", "Hello")

    println("|$result|")
}


/*
run:

|Hello     |

*/

 



answered 3 days ago by avibootz
0 votes
// Right-aligned field

fun main() {
    // Right-align "Kai" in a field 10 characters wide
    val result = String.format("%10s", "Kai")

    println("|$result|")
}


/*
run:

|Hello     |

*/

 



answered 3 days ago by avibootz
0 votes
// Zero-padding numbers

fun main() {
    val number = 123

    // Pad the number with leading zeros to width 8
    val result = String.format("%08d", number)

    println(result)
}


/*
run:

00000123

*/

 



answered 3 days ago by avibootz
0 votes
// Formatting a table

fun main() {

    // Print column headers
    println(
        String.format(
            "%-15s%-10s%-12s",
            "Name",
            "Age",
            "Salary"
        )
    )

    // Print data rows
    println(
        String.format(
            "%-15s%-10d%-12.2f",
            "Oana",
            30,
            64000.0
        )
    )

    println(
        String.format(
            "%-15s%-10d%-12.2f",
            "Gail",
            44,
            71000.0
        )
    )
}
    

/*
run:

Name           Age       Salary      
Oana           30        64000.00    
Gail           44        71000.00    

*/

 



answered 3 days ago by avibootz
0 votes
/**
 * Returns a string padded to a fixed width.
 *
 * value     - value to format
 * width     - field width
 * leftAlign - true for left alignment,
 *             false for right alignment
 */
fun fixedWidth(
    value: Any,
    width: Int,
    leftAlign: Boolean = true
): String {
    return if (leftAlign) {
        String.format("%-${width}s", value)
    } else {
        String.format("%${width}s", value)
    }
}

fun main() {

    println("|${fixedWidth("Hello", 10)}|")
    println("|${fixedWidth(123, 10, false)}|")
}
    

/*
run:

|Hello     |
|       123|

*/

 



answered 3 days ago by avibootz
...