Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Padding with sprintf changing number

by choroba (Archbishop)
on Sep 24, 2021 at 11:17 UTC ( #11136983=note: print w/replies, xml ) Need Help??


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 ??

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-05-21 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?