in reply to Help with decimals
Update: Changed this to be a sub so it doesn't munge the original value as suggested by HyperZonk.#!/usr/bin/perl -w use strict; my $foo; $foo = 1500; print myformat($foo, 2) . "\n"; sub myformat { my ($number, $places) = @_; my $decimal; $decimal = ""; for (1 .. $places) { if ($number) { $decimal .= chop $number; } else { $decimal .= 0; } } return "$number\." . reverse $decimal; }
I also added the reverse because I no-so-brillantly forgot that the decimals would be reversed.
Rich
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Help with decimals
by John M. Dlugosz (Monsignor) on Jul 30, 2001 at 03:09 UTC | |
by rchiav (Deacon) on Jul 31, 2001 at 01:45 UTC |