Decimal field round of

Hi,

I want to round of decimal numbers in comma seperated lines till 2 decimal numbers.

line will be like.

26200000,1,14771,12/31/2016,N,3.542377036225,0.840074561430,-1.236050220213,4.214781311068,,,,,,,,,,,4.635809027565,,,,,,,,,4.635809027565,XXXX,02/09/2017,,,,,,,,,A,XX,10463.580902756539

can you please give me perl code to get the results. I am using below code which rounding off all the decimal fields except the last one which is 10463.580902756539

$decimal_line = sprintf("%.2f", $decimal_line) if($decimal_line =~ /[-+]?[0-9]*(\.[0-9][0-9]+$)/)

Your code is not really the problem. The data is. That field has more precision than a double datatype can handle.

You should consider going to cpan.org, then downloading the BigInt math package, Math::BigInt. You then can use extended floating point variables to handle your problem.

See: Math::BigInt - search.cpan.org

Would this come close to what you need (admittedly, its the not-requested awk , but it shows the principle)?

awk '{for (i=6; i<=NF; i++) if ($i+0 == $i) $i+=0} 1' FS="," OFS="," CONVFMT="%.2f" file
26200000,1,14771,12/31/2016,N,3.54,0.84,-1.24,4.21,,,,,,,,,,,4.64,,,,,,,,,4.64,XXXX,02/09/2017,,,,,,,,,A,XX,10463.58

Do you want the output to be like this?

perl -pe 's/(\.\d\d)\d+/$1/g' vishal0746.example

26200000,1,14771,12/31/2016,N,3.54,0.84,-1.23,4.21,,,,,,,,,,,4.63,,,,,,,,,4.63,XXXX,02/09/2017,,,,,,,,,A,XX,10463.58

or like this?

perl  -pe 's/(\d+\.\d+)/sprintf "%0.2f", $1/ge' example.txt

26200000,1,14771,12/31/2016,N,3.54,0.84,-1.24,4.21,,,,,,,,,,,4.64,,,,,,,,,4.64,XXXX,02/09/2017,,,,,,,,,A,XX,10463.58

Hi.

Like Aia I did not seem have trouble. I used a module that recognizes numerics:

#!/usr/bin/env bash

# @(#) s1       Demonstrate formatting reals.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl dixf

FILE=${1-data1}

pl " Perl code p1:"
cat ./p1

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
./p1 data1

pl " Details for ./p1:"
dixf ./p1

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
perl 5.20.2
dixf (local) 1.42

-----
 Perl code p1:
#!/usr/bin/env perl

# @(#) p1       Demonstrate recognition of reals, round to lower precision.
# Problem with 10463.580902756539
# $decimal_line = sprintf( "%.2f", $decimal_line ) if ( $decimal_line =~ /[-+]?[0-9]*(\.[0-9][0-9]+$)/ ) ;

use strict;
use warnings;
use Regexp::Common qw /number/;

my ( @a, $i );

while (<>) {
  chomp;
  @a = split(/,/);
  for ( $i = 0; $i <= $#a; $i++ ) {
    if ( $a[$i] =~ /^$RE{num}{real}$/ ) {       # format if real
      printf( "%.2f", $a[$i] );
      print "," if $i < $#a;
    }
    else {
      printf( "%s", $a[$i] );
      print "," if $i < $#a;
    }
  }
  print "\n";
}

-----
 Input data file data1:
26200000,1,14771,12/31/2016,N,3.542377036225,0.840074561430,-1.236050220213,4.214781311068,,,,,,,,,,,4.635809027565,,,,,,,,,4.635809027565,XXXX,02/09/2017,,,,,,,,,A,XX,10463.580902756539

-----
 Results:
26200000.00,1.00,14771.00,12/31/2016,N,3.54,0.84,-1.24,4.21,,,,,,,,,,,4.64,,,,,,,,,4.64,XXXX,02/09/2017,,,,,,,,,A,XX,10463.58

-----
 Details for ./p1:
p1      Demonstrate recognition of reals, round to lower precision. (what)
Path    : ./p1
Version : 13.
Length  : 27 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Modules : (for perl codes)
 strict 1.08
 warnings       1.23
 Regexp::Common 2013031301

But comments like ... rounding off all the decimal fields except the last one which is 10463.580902756539 ... that do not explicitly state what the result/problem was does not give us much to go on.

If you had trouble, I wonder if it is a 32-bit problem, but that's just a guess.

Best wishes ... cheers, drl