in reply to number formatting
If you need to pad with a character other than blank or zero you can use one of the following methods. They all generate a pad string with the x operator and combine that with $text. These methods do not truncate $text.Left padding a string with blanks (no truncation: $padded = sprintf("%${pad_len}s", $text); # Right padding a string with blanks (no truncation): $padded = sprintf("%-${pad_len}s", $text); # Left padding a number with 0 (no truncation): $padded = sprintf("%0${pad_len}d", $num); # Right padding a string with blanks using pack (will truncate): $padded = pack("A$pad_len",$text);
Left and right padding with any character, modifying $text directly:$padded = $pad_char x ( $pad_len - length( $text ) ).$text; $padded = $text . $pad_char x ($pad_len - length($text));
Update: Check out the Schwartzian Transform if you need to sort these numbers later!substr($text, 0, 0)=$pad_charx($pad_len-length($text)); $text .= $pad_char x ($pad_len - length($text));
..:::::: aquacade ::::::..
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: number formatting
by elwarren (Priest) on Aug 08, 2001 at 20:06 UTC |