http://www.perlmonks.org?node_id=11136983


in reply to Padding with sprintf changing number

I tried it with 1/3:
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; sub amnt { my ($amount) = @_; say "$amount I'm checking amount before padding"; my $padamnt = sprintf("%016d",$amount); say "$padamnt I'm checking amount after padding"; } amnt(1/3);

As expected, before padding its 0.333333333333333, after padding it's 0000000000000000. Rounding.

Now try it with 1.999999999999999. print doesn't output the precise value.

2 I'm checking amount before padding 0000000000000001 I'm checking amount after padding

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Padding with sprintf changing number
by sachin raj aryan (Acolyte) on Sep 24, 2021 at 11:19 UTC

    how to avoid this ..i dont want rounding after padding

      What about %016.0f?
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Thanks Alot for explanation with example and your worked perfectly i have one more Q...my $padacntnum =sprintf("%017d",$bgl) this is going to round off every number ??