473,379 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Remove integer from float number

How can I return:

".666"

from float:

"0.666"

This is what I have so far:
"%.6f" % x


Thanks Everyone,
Derek Basch

Mar 23 '06 #1
7 1970
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
Mar 23 '06 #2
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

Mar 23 '06 #3
"Derek Basch" <db****@yahoo.com> 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')
Mar 23 '06 #4
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 ...
Mar 23 '06 #5
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
Mar 23 '06 #6
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

Mar 23 '06 #7
On 2006-03-23, Arne Ludwig <ar**@citde.net> 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!
Mar 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Piet | last post by:
Hello wxPythoneers. I have a problem with a dialog box derived from wxFrame which has a wxComboBox as main element. Depending on the entry selected from the ComboBox, the dialog box will be...
8
by: Brent Lievers | last post by:
Greetings, I have a question about parsing a fixed-width integer from a string. Lines of data are being read from a file having a very strict column-delimited format. In my example below,...
4
by: Geky | last post by:
Why when I multiply a float , for example X10, and than convert it as an integer the new value is changed? Example: float fl = 1.8 int x; x = int(fl * 10);
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
8
by: gk245 | last post by:
This is part of a bigger program, but i made it simple (basically the OT_hours function is supposed to determine if the number entered is over 40 hours or not and return the appropriate answer): ...
13
by: kennethlou | last post by:
Hi, If in C a variable appears like X=10.000000, I can round it to zero decimal places. ie X=10? I then have to save the number into a variable. The method appears below:...
1
by: iulian.ilea | last post by:
I have this functions, c3 and c2 that computes and return a float number. The problem is that wen I pass the result of c2 (float number) to c3(), in function apears to be an integer. Ex.:...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.