#!/usr/bin/perl # The Missing Textutils, Ondrej Bojar, obo@cuni.cz # http://www.cuni.cz/~obo/textutils # # 'round' rounds all the numbers it can find to a specified precision (given as # ARG1). # # If prec ≶ 10, rounds up to 'prec' decimal places, # if prec >= 10, rounds to whole "precs" (such as whole tens, thousands...). # # $Id: round,v 1.6 2010-10-31 19:14:53 bojar Exp $ # use strict; use Getopt::Long; my $cz = 0; my $div = undef; my $include_integers = 0; my $suffix = undef; GetOptions( "cz" => \$cz, "include-integers" => \$include_integers, "div=i" => \$div, "suffix=s" => \$suffix, ) or exit 1; my $prec = shift; $prec = 2 if !defined $prec; sub nicenum { # Czech numbers typography my $str = shift; $str =~ s/,/./; $str = $str / $div if defined $div; my $out; if ($prec < 10) { $out = sprintf("%.${prec}f",$str); } else { $out = $prec*sprintf("%.0f",$str/$prec); } $out =~ s/\./,/ if $cz; $out .= $suffix if defined $suffix; return $out; } while (<>) { if ($include_integers) { s/([0-9]+([\.,][0-9]+)?)/nicenum $1/ge; } else { s/([0-9]+\.[0-9]+)/nicenum $1/ge; s/([0-9]+\,[0-9]+)/nicenum $1/ge if $cz; } print; }