| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# numround: A program that rounds off numbers it encounters. |
| 4 |
# |
| 5 |
# Copyright (C) 2002-2004 Suso Banderas |
| 6 |
|
| 7 |
# This program is free software; you can redistribute it and/or |
| 8 |
# modify it under the terms of the GNU General Public License |
| 9 |
# as published by the Free Software Foundation; either version 2 |
| 10 |
# of the License, or (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program; if not, write to the Free Software |
| 19 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 |
# |
| 21 |
# You may contact the author at <[email protected]>. |
| 22 |
|
| 23 |
####################### |
| 24 |
# VARIABLES AND SETUP # |
| 25 |
####################### |
| 26 |
|
| 27 |
use Getopt::Std; |
| 28 |
use strict; |
| 29 |
use vars qw/ %opts $verbose /; |
| 30 |
use POSIX; # for rounding functions. |
| 31 |
|
| 32 |
getopts('cfn:hdV', \%opts); |
| 33 |
|
| 34 |
my ($file, @number_array, $verbose); |
| 35 |
|
| 36 |
if ($opts{'h'}) { |
| 37 |
&help(); |
| 38 |
exit(0); |
| 39 |
} |
| 40 |
|
| 41 |
if ($opts{'d'}) { |
| 42 |
$verbose = 3; |
| 43 |
print STDERR "Debug mode\n"; |
| 44 |
} elsif ($opts{'V'}) { |
| 45 |
$verbose = 2; |
| 46 |
print STDERR "Verbose mode\n"; |
| 47 |
} elsif ($opts{'q'}) { |
| 48 |
$verbose = 0; # Nothing except the final answer |
| 49 |
} else { |
| 50 |
$verbose = 1; # Normal warnings and such. |
| 51 |
} |
| 52 |
|
| 53 |
# If the -n option is set, use the factor from that, otherwise use a factor |
| 54 |
# of 1 for standard rounding. |
| 55 |
my $factor = $opts{'n'} || 1; |
| 56 |
|
| 57 |
################ |
| 58 |
# MAIN PROGRAM # |
| 59 |
################ |
| 60 |
|
| 61 |
# For file args |
| 62 |
if (@ARGV) { |
| 63 |
foreach $file (@ARGV) { |
| 64 |
print STDERR "Reading from file $file.\n" if ($verbose >= 2); |
| 65 |
open (ARGFILE, "$file") && |
| 66 |
process_filehandle(\*ARGFILE, \@number_array) || |
| 67 |
$verbose && warn "Couldn't open file $file for reading: $!\n"; |
| 68 |
close (ARGFILE); |
| 69 |
} |
| 70 |
# For STDIN |
| 71 |
} else { |
| 72 |
print STDERR "Reading from STDIN.\n" if ($verbose >= 2); |
| 73 |
process_filehandle(\*STDIN, \@number_array); |
| 74 |
} |
| 75 |
|
| 76 |
#process_numbers(\@number_array); |
| 77 |
|
| 78 |
exit(0); |
| 79 |
|
| 80 |
############### |
| 81 |
# SUBROUTINES # |
| 82 |
############### |
| 83 |
|
| 84 |
sub help { |
| 85 |
print <<"EOF"; |
| 86 |
--------------------------------------------------------- |
| 87 |
numround : A program that rounds off numbers it encounters. |
| 88 |
--------------------------------------------------------- |
| 89 |
Usage: |
| 90 |
|
| 91 |
numround [options] <file> |
| 92 |
| numround [options] |
| 93 |
numround [options] |
| 94 |
|
| 95 |
Options: |
| 96 |
-c Force the number to be rounded up. Ceiling. |
| 97 |
-f Force the number to be rounded down. Floor. |
| 98 |
-n <n> Round numbers to the nearest factor of <n>. |
| 99 |
|
| 100 |
-d Debug. For developers only. |
| 101 |
-h Help: You're looking at it. |
| 102 |
-V Increase verbosity. |
| 103 |
EOF |
| 104 |
} |
| 105 |
|
| 106 |
sub process_filehandle { |
| 107 |
my $filehandle = shift; |
| 108 |
my $number_array_ref = shift; |
| 109 |
|
| 110 |
while (<$filehandle>) { |
| 111 |
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) { |
| 112 |
my $number = $1; |
| 113 |
print STDERR "found number: $1\n" if ($verbose >= 3); |
| 114 |
my $newnumber; |
| 115 |
print STDERR "original number: $number\n" if ($verbose >= 2); |
| 116 |
if ($opts{'c'}) { |
| 117 |
$newnumber = ceiling_number($number); |
| 118 |
} elsif ($opts{'f'}) { |
| 119 |
$newnumber = floor_number($number); |
| 120 |
} else { |
| 121 |
$newnumber = round_number($factor, $number); |
| 122 |
} |
| 123 |
print "$newnumber\n"; |
| 124 |
} |
| 125 |
} |
| 126 |
return 1; |
| 127 |
} |
| 128 |
|
| 129 |
# The simple rounding function. In the next version, this function |
| 130 |
# will be able to round to arbitrary factors of any number. |
| 131 |
# The algorithm for this subroutine was inspired by the nearest subroutine |
| 132 |
# in Geoffrey Rommel's Math::Round perl module. Thanks Geoffrey. |
| 133 |
sub round_number { |
| 134 |
my $factor = shift; |
| 135 |
my $number = shift; |
| 136 |
my $rounded_number; |
| 137 |
$factor = abs($factor); |
| 138 |
|
| 139 |
if ($number >= 0) { |
| 140 |
$rounded_number = $factor * floor(($number + 0.5 * $factor) / $factor); |
| 141 |
} else { |
| 142 |
$rounded_number = $factor * ceil(($number - 0.5 * $factor) / $factor); |
| 143 |
} |
| 144 |
return $rounded_number; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
sub ceiling_number { |
| 149 |
my $number = shift; |
| 150 |
my $ceiling_number = ceil($number); |
| 151 |
return $ceiling_number; |
| 152 |
} |
| 153 |
|
| 154 |
sub floor_number { |
| 155 |
my $number = shift; |
| 156 |
my $floor_number = floor($number); |
| 157 |
return $floor_number; |
| 158 |
} |
| 159 |
|
| 160 |
# Lay down some of that perl pod action. |
| 161 |
=pod |
| 162 |
|
| 163 |
=head1 NAME |
| 164 |
|
| 165 |
numround - A program that rounds off numbers it encounters. |
| 166 |
|
| 167 |
=head1 SYNOPSIS |
| 168 |
|
| 169 |
B<numround> [-cdfhV] <FILE> |
| 170 |
|
| 171 |
| B<numround> [-cdfhV] (Input on STDIN from pipeline.) |
| 172 |
|
| 173 |
B<numround> [-cdfhV] (Input on STDIN. Use Ctrl-D to stop.) |
| 174 |
|
| 175 |
=head1 DESCRIPTION |
| 176 |
|
| 177 |
B<numround> |
| 178 |
will simply round decimal numbers to the nearest integer or to a factor of any |
| 179 |
number using the -n option. You can also force it to round up (ceiling) or |
| 180 |
round down(floor) using the -c and -f options. |
| 181 |
|
| 182 |
=head1 OPTIONS |
| 183 |
|
| 184 |
-c Force the number to be rounded up. Ceiling. |
| 185 |
-f Force the number to be rounded down. Floor. |
| 186 |
-n <n> Round numbers to the nearest factor of <n>. |
| 187 |
|
| 188 |
-h Print out some helpful information about numround. |
| 189 |
-V Increase verbosity. This will print out numbers as it finds them. |
| 190 |
-d Debug mode. For developers only. |
| 191 |
|
| 192 |
|
| 193 |
=head1 BUGS |
| 194 |
|
| 195 |
B<numround> currently only rounds the first number on each line. Eventually this |
| 196 |
will be changed as all the numeric utilities are developed. |
| 197 |
|
| 198 |
B<numround> will drop off the decimal places in decimal numbers. This may cause |
| 199 |
some calculations to be in error, depending on how you are using the data. |
| 200 |
|
| 201 |
=head1 SEE ALSO |
| 202 |
|
| 203 |
numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numrange(1) |
| 204 |
|
| 205 |
=head1 COPYRIGHT |
| 206 |
|
| 207 |
numround is part of the num-utils package, which is copyrighted by |
| 208 |
Suso Banderas and released under the GPL license. Please read |
| 209 |
the COPYING and LICENSE files that came with the num-utils package |
| 210 |
|
| 211 |
Developers can read the GOALS file and contact me about providing |
| 212 |
submitions or help for the project. |
| 213 |
|
| 214 |
=head1 MORE INFO |
| 215 |
|
| 216 |
More info on numround can be found at: |
| 217 |
|
| 218 |
=over 1 |
| 219 |
|
| 220 |
=item B<http://suso.suso.org/programs/num-utils/> |
| 221 |
|
| 222 |
=back |
| 223 |
|
| 224 |
=cut |
| 225 |
|