in reply to Numbers....
There are may ways to do it, for example, if you are familiar with C, you might like to use printf() or sprintf().
I will show you another way, simply using string formatting :
my $number = 10.8766756; my $string = $number . ""; # force $number into a string $string =~ s/(\.\d)\d*/\1/; # if $string has one (or more) decimal # figures, replace them all by the first decimal figure
Another one is the purely mathematical method, which might (or might not)
work, depending on the size of your numbers (notice that
arithmetic with floating point numbers is not always exact) :
The int() function returns the integer
part of a number, so we simply calculate
which will also give you 10.8, but note that the number is not rounded but truncated to 0, as the int documentation will tell you.$string = int( $number * 100 ) / 100;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Methods to format a number
by nuance (Hermit) on Jun 02, 2000 at 19:13 UTC | |
by Corion (Patriarch) on Jun 02, 2000 at 19:20 UTC | |
by Adam (Vicar) on Jun 02, 2000 at 21:07 UTC |