Remove integer from float number

D

Derek Basch

How can I return:

".666"

from float:

"0.666"

This is what I have so far:

Thanks Everyone,
Derek Basch
 
L

Larry Bates

Derek said:
How can I return:

".666"

from float:

"0.666"

This is what I have so far:


Thanks Everyone,
Derek Basch

This works but I'm not entirely sure I know what you are
trying to accomplish.

("%.3f" % x)[1:]


-Larry Bates
 
D

Derek Basch

Ahh yes you have to put parenthases around the string formatting to
remove the integer using indexes. Thanks, that's just what I needed!

Derek Basch
 
P

Paul Rubin

Derek Basch said:
Ahh yes you have to put parenthases around the string formatting to
remove the integer using indexes. Thanks, that's just what I needed!

I think it's better to remove leading zeros explicitly:

('%.3x' % x).lstrip('0')
 
J

John Machin

Derek said:
How can I return:

".666"

from float:

"0.666"

This is what I have so far:



Thanks Everyone,
Derek Basch


This works but I'm not entirely sure I know what you are
trying to accomplish.

("%.3f" % x)[1:]
>>> x = 12345.666; ("%.3f" % x)[1:] '2345.666'
>>>

I'm sure of neither what the OP is trying to accomplish nor what Larry's
definition of "works" is :)

Perhaps the condition abs(x) < 1.0 is implied ...
 
L

Larry Bates

John said:
Derek said:
How can I return:

".666"

from float:

"0.666"

This is what I have so far:


"%.6f" % x

Thanks Everyone,
Derek Basch


This works but I'm not entirely sure I know what you are
trying to accomplish.

("%.3f" % x)[1:]
x = 12345.666; ("%.3f" % x)[1:] '2345.666'

I'm sure of neither what the OP is trying to accomplish nor what Larry's
definition of "works" is :)

Perhaps the condition abs(x) < 1.0 is implied ...

For the example given, my code works. With so little information
the only thing I could do is answer the specific question and
caveat it that I don't know "exactly" what OP is trying to accomplish.
By the OPs response to my post, it was what he was looking for.
But I agree it is very much an edge-case question.

-Larry Bates
 
A

Arne Ludwig

With that terse description and the subject line I would interpret the
OP like so:
..666
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top