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 JavaScript

6 Answers

0 votes
/**
 * Formats a record using fixed-width fields.
 *
 * Name   : left-aligned, width 15
 * Age    : right-aligned, width 10
 * Salary : right-aligned, width 12, 2 decimal places
 */
function formatRecord(name, age, salary) {
    return (
        name.padEnd(15) +
        String(age).padStart(10) +
        salary.toFixed(2).padStart(12)
    );
}

// Store the formatted string in a variable
const result = formatRecord("Alice", 31, 65000);

console.log(result);


/*
run:

Alice                  31    65000.00

*/

 



answered 3 days ago by avibootz
0 votes
// Left-align "Hello" in a field 10 characters wide
const result = "Hello".padEnd(10);

console.log("|" + result + "|");


/*
run:

|Hello     |

*/

 



answered 3 days ago by avibootz
0 votes
// Right-align "Tom" in a field 10 characters wide
const result = "Tom".padStart(10);

console.log("|" + result + "|");


/*
run:

|       Tom|

*/

 



answered 3 days ago by avibootz
0 votes
// Convert the number to a string and pad with leading zeros
const result = String(123).padStart(8, "0");

console.log(result);


/*
run:

00000123

*/

 



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

// Print column headers
console.log(
    "Name".padEnd(15) +
    "Age".padEnd(10) +
    "Salary".padEnd(12)
);

// Print data rows
console.log(
    "Alice".padEnd(15) +
    String(30).padEnd(10) +
    (75000).toFixed(2).padEnd(12)
);

console.log(
    "GoGo".padEnd(15) +
    String(42).padEnd(10) +
    (82000).toFixed(2).padEnd(12)
);



/*
run:

Name           Age       Salary      
Alice          30        75000.00    
GoGo           42        82000.00    

*/

 



answered 3 days ago by avibootz
0 votes
/**
 * Returns a fixed-width string.
 *
 * value : value to format
 * width : field width
 * left  : true for left alignment, false for right alignment
 */
function fixedWidth(value, width, left = true) {
    value = String(value);

    return left
        ? value.padEnd(width)
        : value.padStart(width);
}

console.log("|" + fixedWidth("Hello", 10) + "|");
console.log("|" + fixedWidth(123, 10, false) + "|");



/*
run:

|Hello     |
|       123|

*/

 



answered 3 days ago by avibootz
...