[CentralOH] Plucking Digits From Float

Joe Shaw joe at joeshaw.org
Mon Dec 9 15:57:31 CET 2013


Hi,

It's worth pointing out also that floating point values are an
approximation.  This is one reason why you should never do direct
comparisons with floating point numbers, but it could also bite you
here.  It's possible that 1.2346 (or any value you choose) cannot be
precisely represented as you wish, and so converting it to a string
and taking the negative index might be very, very wrong.

0.1 is one such number, and it can be demonstrated in python on some
platforms.  Go into the Python REPL and type "0.1" and you might get
"0.10000000000000001" back.

If this is really important you can store your values as integers --
which are exactly precise -- and handle the decimals yourself.  (In
other words, your own implemention of fixed point values.)

Joe

On Sun, Dec 8, 2013 at 2:40 PM,  <jep200404 at columbus.rr.com> wrote:
> On Sun, 8 Dec 2013 14:36:01 -0500, Louis Bogdan <looiebwv at gmail.com> wrote:
>
>> num is calculated value 1.2346
>> a=num(-1) is indicating the numeral "6" with negative indexing notation.
>> b=num(-2) is indicating the numeral "4"   "         "
>
> I question the sanity of do so. Nonetheless.
>
> num = 1.2346
> a = str(num)[-1]
> b = str(num)[-2]
>
> print num, a, b
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh


More information about the CentralOH mailing list