Click to See Complete Forum and Search --> : Decimal Math


waypoon
11-17-2004, 04:52 AM
Do yo know that: I want MATH near to 10
$math % 10 == 0, but decimal can not use this code!


$a = <STDIN>;
$b = $a % 10;
if (!($b == 0)) {
$c = $a + (10 - $b);
} else { $c = "$a"; }


Ans: $a = 21 ; $c = 30
Ans: $a = 29 ; $c = 30
Ans: $a = 30 ; $c = 30


BUT if support decimal, how to write it

$a = 21.001 ; $c = 21.010
$a = 21.011 ; $c = 21.020
$a = 21.110 ; $c = 21.200
$a = 21.200 ; $c = 22.000



*can no use (<=5 out) (>6 in) this effect
is 0.45 = 0.40
is 0.65 = 0.70
is NO this effect

NogDog
11-17-2004, 09:28 AM
#/usr/bin/perl -w
use strict;
# round to nearest 10:
my $a = <STDIN>;
chomp($a);
printf("%d\n", sprintf("%.0f", $a/10) * 10);
# round to nearest .01:
$a = <STDIN>;
chomp($a);
printf("%.2f\n", $a);