in reply to Using printf with %.1f

alexsc01,

Use a custom format sub such as the following I wrote:

my $num = 244.83; print "'" . str($num, 8, 3) . "'\n"; sub str { ## This sub is used to return a formatted numeric string. ## It has three arguments which are: the number sent, the ## desired width (including decimal point, if any), and ## the number of decimal places (optional) in that order. ## The number is right-justified. my ($sentnum, $width, $precision) = @ARG; ## Check to see if a non-zero precision was sent. if ($precision) { ## print to var using decimal point. $sentnum = sprintf("%" . "$width.$precision" . "f", $sentnum); } else { ## print in integer format. $sentnum = sprintf("%" . "$width.0" . "f", $sentnum); } ## change decimals to commas $sentnum =~ tr/./,/; return $sentnum; }
Hope this helps.

Update: could move tr/./,/ to above else statement.
Results: ' 244,830'