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 Ruby

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 # Float, 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 float, width 12,
#           with 2 digits after the decimal point
def format_record(name, age, salary)
  sprintf("%-15s%10d%12.2f", name, age, salary)
end

# Store the formatted string in a variable
result = format_record("Nicola", 27, 95_000.0)

puts result



# run:
#
# Nicola                 27    95000.00
#

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
0 votes
# Left-align "Hello" in a field 10 characters wide
result = sprintf("%-10s", "Hello")

puts "|#{result}|"



# run:
#
# |Hello     |
#

 



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

# Right-align "Ann" in a field 10 characters wide
result = sprintf("%10s", "Ann")

puts "|#{result}|"



# run:
#
# |       Ann|
#

 



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

# Pad an integer with leading zeros to width 8
result = sprintf("%08d", 123)

puts result


# run:
#
# 00000123
#

 



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

# Print column headers
puts sprintf("%-15s%-10s%-12s", "Name", "Age", "Salary")

# Print data rows
puts sprintf("%-15s%-10d%-12.2f", "Ben", 60, 135_000.0)
puts sprintf("%-15s%-10d%-12.2f", "Dee",   71, 178_000.0)


# run:
#
# Name           Age       Salary      
# Ben            60        135000.00   
# Dee            71        178000.00   
#

 



answered 3 days ago by avibootz
0 votes
# Returns a string padded to a fixed width.
#
# value      - value to format
# width      - field width
# left_align - true for left alignment,
#              false for right alignment
def fixed_width(value, width, left_align = true)
  if left_align
    sprintf("%-#{width}s", value)
  else
    sprintf("%#{width}s", value)
  end
end

puts "|#{fixed_width('Hello', 10)}|"
puts "|#{fixed_width(123, 10, false)}|"


# run:
#
# |Hello     |
# |       123|
#

 



answered 3 days ago by avibootz
...