#!/usr/bin/perl -w use 5.016; # rounded-1079543.pl - Round to 2 decimal places using regexen # (even though printf/sprintf would be preferred) # update 2: as Laurent_R notes, this is truncation, not ro +unding. # but it is what OP asked for. my $num = 3.333333333; $num =~ /(\d*\.\d{2})/; my $roundednum = $1; say $roundednum; # or my $num2 = 5.5555555; $num2 =~ s/(\d*\.)(\d)(\d).*/$1$2$3/; my $rounded2 = $num2; say $rounded2; =head execution: C:>rounded-1079543.pl 3.33 5.55 =cut
Reformatted (belatedly): added code tags when the (well-deserved) downvotes accumulated sufficiently to attract my attention.
Is my impression that answers submitted to the Q&A editors are NOT editable by the author, until approved into public use?
If so, maybe we should make the input box for Q&A look like (and behave like) those for SOPW, etc.
In reply to Re: Rounding numbers
by ww
in thread Rounding numbers
by Dr Manhattan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |