473,320 Members | 1,810 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,320 software developers and data experts.

Change from an integer to a decimal

Sushi
19
Heyo all,

Having a problem which seems really stupid but I just cant solve it.

Im reading in a number of integers from a file e.g. 179, 23, 56
I then have to divde them by 255, but when I do I only ever get values of 0 or 1 rather than something like 0.56, 0.79. I thought I could just convert the number to a float before dividing it e.g a = 179 therefore float(a), but that doesnt seem to work, and Ive looked about on the web but cant find an answer.

Any help would be most appreciated thanks!
Mel
Mar 5 '07 #1
5 2533
dshimer
136 Expert 100+
float() one or both of the numbers before the division, or just use a real for the denominator. If either number is a real, then the answer will be a real

Expand|Select|Wrap|Line Numbers
  1. >>> 255/178
  2. 1
  3. >>> 255/178.0
  4. 1.4325842696629214
  5. >>> float(255)/178
  6. 1.4325842696629214
  7. >>> 
  8.  
Mar 5 '07 #2
ghostdog74
511 Expert 256MB
you can also run your script like this
Expand|Select|Wrap|Line Numbers
  1.  python -Qnew yourscript.py
  2.  
or in your script, import __future__

Expand|Select|Wrap|Line Numbers
  1. from __future__ import division
  2. print 255/178
  3.  
Mar 5 '07 #3
bvdet
2,851 Expert Mod 2GB
you can also run your script like this
Expand|Select|Wrap|Line Numbers
  1.  python -Qnew yourscript.py
  2.  
or in your script, import __future__

Expand|Select|Wrap|Line Numbers
  1. from __future__ import division
  2. print 255/178
  3.  
This is new to me so I looked it up. Presently the '/' operator is mapped to object method __div__(). In the future it will be remapped to __truediv__(), which evaluates integer division to float.

__truediv__() can also be accessed thus:
Expand|Select|Wrap|Line Numbers
  1. >>> from operator import truediv
  2. >>> truediv(1,2)
  3. 0.5
  4. >>> truediv(21,7)
  5. 3.0
  6. >>> 
Importing division enables this feature.
Mar 6 '07 #4
bartonc
6,596 Expert 4TB
Sushi: Great to have you back after so long.
You other three: You guys are awesome. Great posts!
Mar 6 '07 #5
Sushi
19
Thank you all very much for your help!
I wasn't floating both numbers, only the ones that were being read in from the file which is why it wasnt working. So again many thanks for the help!

Barton: Good to be back! I've been working on using Blender quite a lot, and whilst it's a very nice piece of software isnt exactly the easiest thing to use with python!

Mel
Mar 6 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
3
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
17
by: henrytcy | last post by:
Hi, How can I get a digit from integer for example, 12311, one by one for comparision? Thanks!
3
by: vanidosa27 | last post by:
I need to write a code that calculates the change due after the user has input the amount owed and amount paid. This is easy enough! :rolleyes: My problem comes in when the program wants you to...
5
by: max | last post by:
There is probably an easy answer to this but I can't figure it out. I have a table with fields of numbers that are 5 or more decimals long. i.e. 47.19568 I want to round these numbers to 1...
11
by: ChrisM | last post by:
Hi Guys, I'm looking for a function that will return the lowest integer that is greater than or equal to a decimal number. eg. 3.1(decimal) returns 4(integer) 3.9(decimal) returns...
1
by: jmarr02s | last post by:
I am trying to change my Amount column Data Type from Integer to Decimal (precision 9 digits, scale 3, that is 6 digits to the left of decimal and 3 digits to the right of decimal. Here is the...
6
by: kimiraikkonen | last post by:
Hi, Sorry if this is so basic but i wanted to know the correct way of displaying a value's "only" integer part. I mean; if i declare a value as integer which is actually a decimal number, it...
3
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the...
3
by: Jonathan Wood | last post by:
Er... When it's a Decimal. I'm executing a query that includes the following: "INSERT INTO ; SELECT SCOPE_IDENTITY()" I then attempt to retrieve the scope identity with code that looks like...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.