I frankly don't understand why you should have an internal representation of the number with the decimal zero (either you want to print it or you want to compute with it, but maybe I'm wrong). Anyway the right way to go is using sprintf() to generate a correctly truncated string and then rely on Perl's ability to cast numbers in the most appropriate ways.
Here is an example:
use strict;
while ( <DATA> ) {
my $val = sprintf( "%.01f", $_/1000 );
# do maths with the truncated result....
my $val2 = $val * 2;
print "$val - $val2\n";
}
__DATA__
100
1000
2314
171
123456789
The result is
0.1 - 0.2
1.0 - 2
2.3 - 4.6
0.2 - 0.4
123456.8 - 246913.6
Note how the .0 is lost if you do any maths on 1.0 unless you format it again before printing - the first column is made of strings, the second of floats.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.