Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: finding the correct integer

by BillKSmith (Prior)
on Nov 30, 2020 at 16:10 UTC ( #11124401=note: print w/replies, xml ) Need Help??


in reply to finding the correct integer

The "%f" format does rounding to the specified precision. In this case use:
print sprintf("%4.0f\n", 0.6505 / 0.0001);

OUTPUT:

6505
Bill

Replies are listed 'Best First'.
Re^2: finding the correct integer
by LanX (Cardinal) on Nov 30, 2020 at 18:20 UTC
    Yes, it does in the case the OP provided.

    But it's important to keep in mind that accumulated rounding errors for longer calculations won't be magically compensated.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      LanX and haukex have the "right" answer here. Sure, various things can be done to coax "the right answer" out of "this example," but the underlying mathematical issues are still there and can't be evaded.

        you continue to add nothing of value to any discussion. Why do you bother?

Re^2: finding the correct integer
by LanX (Cardinal) on Nov 30, 2020 at 22:10 UTC
    another nitpick, the OP asked for int not rounding

    please compare:

    DB<6> print int 0.65049 / 0.0001; 6504 DB<7> printf("%4.0f\n", 0.65049 / 0.0001); 6505 DB<8>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2021-04-10 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?