THis one is for all you Computer Science majors. Part of a script i'm writing loops through fields of an array that are known to be numeric and rounds them to the nearest tenth using the sprintf() function. The problem is, some of the items are rounded correctly, others are not. Here's an example
@num_fields is an array containing the positions in the array that are numeric and need to be rounded
@line is the array of information to fix.
foreach (@num_fiels)
{
$line[$_] = $line[$_]=~ /^$/ ? $line[$_] : sprintf("%.1f",$line[$_]);
}
THis should basically run through the array and if it's not null, round the value to the nearest tenth of whatever is in it. However, as shown below i get variable outputs based on inputs.
Input: Output:
-33.05 -33.0
-23.05 -23.1
5.15 5.2
4.65 4.7
33.05 33.0
Can anyone explain to me why Perl would round it down one time and then up the next? It seems slightly random to me.
-BStopp
@num_fields is an array containing the positions in the array that are numeric and need to be rounded
@line is the array of information to fix.
foreach (@num_fiels)
{
$line[$_] = $line[$_]=~ /^$/ ? $line[$_] : sprintf("%.1f",$line[$_]);
}
THis should basically run through the array and if it's not null, round the value to the nearest tenth of whatever is in it. However, as shown below i get variable outputs based on inputs.
Input: Output:
-33.05 -33.0
-23.05 -23.1
5.15 5.2
4.65 4.7
33.05 33.0
Can anyone explain to me why Perl would round it down one time and then up the next? It seems slightly random to me.
-BStopp