in reply to Filthy Floats
# rounds to 0.01 (assumes positive number): my $cleanFloat = sprintf "%.2f", $dirtyFloat + 0.004; # truncates to 0.01: my $cleanFloat = sprintf "%.2f", $dirtyFloat;
update: it appears that sprintf internally rounds, which makes my "truncates" example not do so. However, the "rounds" example may be better, as it gets around sprintf's buggy rounding.
update2: but since sprintf does buggy rounding for 0.5, it would be better to add 0.004; changed this.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Filthy Floats
by Hofmator (Curate) on Jul 16, 2001 at 20:53 UTC | |
by scain (Curate) on Jul 16, 2001 at 21:14 UTC | |
by I0 (Priest) on Jul 17, 2001 at 10:56 UTC | |
by Hofmator (Curate) on Jul 17, 2001 at 14:13 UTC | |
by iakobski (Pilgrim) on Jul 17, 2001 at 15:34 UTC | |
by I0 (Priest) on Jul 17, 2001 at 22:16 UTC | |
|
Re: Re: Filthy Floats
by abstracts (Hermit) on Jul 16, 2001 at 21:12 UTC |