Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Padding with sprintf changing number

by syphilis (Archbishop)
on Sep 27, 2021 at 14:26 UTC ( [id://11137041]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Padding with sprintf changing number
in thread Padding with sprintf changing number

I've rewritten your code into a form that I think makes at least some sense:
use strict; use warnings; my $amt = 4887.15; print " Checking amnt before conversion $amt,\n"; $amt= sprintf("%.2f",$amt); print " Checking amnt after rounding $amt,\n"; $amt = $amt*100; print " Checking amnt after conversion $amt,\n"; printf "Checking the EXACT value of amnt: %.17g\n", $amt; amnt($amt); sub amnt { my $amount=$_[0]; $amount=int($amount); print "$amount i m checking amount before padding,\n"; #my $padamnt = sprintf("%016.0f",$amount);---> currently commented to +check integer effect. my $padamnt = sprintf("%016d",$amount); print "$padamnt i m checking amount after padding,\n"; return $padamnt; }
When I run that script, I get:
Checking amnt before conversion 4887.15, Checking amnt after rounding 4887.15, Checking amnt after conversion 488715, Checking the EXACT value of amnt: 488714.99999999994 488714 i m checking amount before padding, 0000000000488714 i m checking amount after padding,
Note that I've added a line of code that demonstrates that the EXACT value of $amt is NOT 488715.
Perl's print() function frequently fails to produce an accurate representation of floating point values - and that's what is happening here.

That output I'm seeing is as I expect.
Which is the line of output that you don't understand ?

You need to stay alert to this aspect of perl's print() function.
Neither python3 nor raku are afflicted with such a poorly designed implementation:
$ python3 -c "print(4887.15 * 100)" 488714.99999999994 $ raku -e "say 4887.15e0 * 100" 488714.99999999994


Cheers,
Rob

Replies are listed 'Best First'.
Re^4: Padding with sprintf changing number
by choroba (Cardinal) on Sep 27, 2021 at 14:31 UTC
    > Neither python3 nor raku

    But

    $ python2 -c 'print(4887.15 * 100)' 488715.0

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Yes, I had good reason to avoid mentioning python2's print() implementation ;-)
      It apparently rounds to even fewer decimal digits than perl5:
      $ python2 -c 'print 2 ** 0.5' 1.41421356237 $ perl -le 'print 2 ** 0.5' 1.4142135623731
      At least they've done something about it.
      Maybe perl7 will do the same.

      Cheers,
      Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2025-01-20 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (59 votes). Check out past polls.