/*
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
*/
import Foundation
/// 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
func formatRecord(name: String, age: Int, salary: Double) -> String {
let nameField = name.padding(
toLength: 15,
withPad: " ",
startingAt: 0
)
let ageField = String(format: "%10d", age)
let salaryField = String(format: "%12.2f", salary)
return nameField + ageField + salaryField
}
// Store the formatted string in a variable
let result = formatRecord(
name: "Jean",
age: 71,
salary: 210000.0
)
print(result)
/*
run:
Jean 71 210000.00
*/