Monday, November 10, 2008

Perl: Rounding off Decimal Digits


#!/usr/bin/perl
# Filename: kb_converter.pl
# Description: Convert values from Bytes to KiloBytes
use strict;

my $val;
my $argc = @ARGV;
if($argc == 1) {
$val = shift;
} else {
$val = 12345;
}

# 1KB = 1024Bytes
my $val_kb = $val / 1024;
my $val_kb_rounded = sprintf("%.2f", $val / 1024);
print "Not Rounded Answer:\n$val Bytes = $val_kb KB\n";
print "Rounded Answer:\n$val Bytes = $val_kb_rounded KB\n";

No comments: