Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Remove integer from float number

Reply
Thread Tools

Remove integer from float number

 
 
Derek Basch
Guest
Posts: n/a
 
      03-23-2006
How can I return:

".666"

from float:

"0.666"

This is what I have so far:

>>> "%.6f" % x


Thanks Everyone,
Derek Basch

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      03-23-2006
Derek Basch wrote:
> 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:]


-Larry Bates
 
Reply With Quote
 
 
 
 
Derek Basch
Guest
Posts: n/a
 
      03-23-2006
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

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-23-2006
"Derek Basch" <> writes:
> 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')
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-23-2006
On 24/03/2006 6:44 AM, Larry Bates wrote:
> Derek Basch wrote:
>
>>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 ...
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      03-23-2006
John Machin wrote:
> On 24/03/2006 6:44 AM, Larry Bates wrote:
>> Derek Basch wrote:
>>
>>> 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
 
Reply With Quote
 
Arne Ludwig
Guest
Posts: n/a
 
      03-23-2006
With that terse description and the subject line I would interpret the
OP like so:

>>> print re.sub(".*\.",".","0.666")

..666
>>> print re.sub(".*\.",".","123.666")

..666

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      03-23-2006
On 2006-03-23, Arne Ludwig <> wrote:
> With that terse description and the subject line I would interpret the
> OP like so:
>
>>>> print re.sub(".*\.",".","0.666")

> .666
>>>> print re.sub(".*\.",".","123.666")

> .666


Or if you're allergic to regular expressions:

>>> print "." + "0.666".split(".")[-1]

..666
>>> print "." + "123.666".split(".")[-1]

..666
>>>




--
Grant Edwards grante Yow! Yow! It's a hole
at all the way to downtown
visi.com Burbank!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
RE: Remove integer from float number Michael Yanowitz Python 5 03-25-2006 02:55 AM
RE: Remove integer from float number Michael Yanowitz Python 1 03-23-2006 10:29 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57