The title doesn't really make sense, as a number with some thing like .23238382 attached to the back of it isn't an integer, but that's not the point. I want to make a program that places commas in the correct position in a number you give. Well, in some of the division equations I need to do, some of the numbers have a decimal on the back and a bunch of numbers, which throws off the program, so I can't seem to figure out a way to take the decimal and numbers behind it off of the number. Any help would be greatly appreciated :)

if the number in str format contains a '.' character use split()

numsplit = str(num).split('.')
num_int = int(numsplit[0])
num_dec = numsplit[1]  # this is a string

If all you want to do is remove the floating point digits you could do this:

floatValue=3.141
integerValue = int(floatValue)
print "The integer equivalent of", floatValue, "is", integerValue

Note: The above code is python 2.x syntax. For python 3.x change the print statement to:

print("The integer equivalent of", floatValue, "is", integerValue)

All this does is create a new integer based on the value of the floatValue. Anything after the decimal point is discarded in the integerValue.

Alternatively, because python variables can hold any data-type, there's nothing stopping you from doing this:

value=3.141
print "The initial value is:", value
value = int(value)
print "The value is now:", value

Again, if you're using python 3.x, you'll need to alter the print statements.

All the above code segment does is converts the float value into an integer. So initially value holds a float, but line 3 changes it to an int. Again, anything after the decimal point is simply discarded.

Another alternative is rounding using math.floor() or math.ceil().
math.floor will round down to the nearest whole number and math.ceil will round up to the nearest whole number.
Using floor or ceil will allow your values to remain as floats.

import math

value=3.1412
print "Value is:", value
print "math.ceil() rounds value up to: ", math.ceil(value)
print "math.floor() rounds value down to:", math.floor(value)

Again, this was 2.x syntax, alter the print statements if you're using 3.x.

The thing to bear in mind with all of these examples is that you are altering your values by either ignoring the decimal point completely (as in the first example) or you are rounding the values up or down.

You mentioned something about problems with division.. If your program is having problems with rounding errors, due to the inherent problems with floating point arithmetic then there is a class which can help!
Decimal, which is in the decimal package is a high precision class for performing exact floating point calculations.

Here's a very quick example of using the Decimal class:

from decimal import *

d1 = Decimal("49925.3304294852348432")
d2 = Decimal("500.000340023")
d3 = d1/d2
print d3    # use print(d3) if you're using python 3.x!

If you have any ints or floats that you want to use in a decimal calculation, you have to convert them to a string.
e.g.

import decimal as dc

calculatedFloat = 2.0002553001
numberToDivide = 100

d1 = dc.Decimal(str(numberToDivide))
d2 = dc.Decimal(str(calculatedFloat))
d3 = d1/d2
print d3

There are various other things that you can set with the Decimal class like the precision (or the number of decimal places!) for example..This snippet by vegaseat shows how to set the precision of the Decimal class:
http://www.daniweb.com/code/snippet217014.html

Anyway, I hope that has helped in some way.
Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: n/a

Looking at your original post again, if it's just a case of removing the stuff after the decimal point and then adding your commas in the appropriate place, i'd probably do the following:

If you're perfoming your calculations using ordinary floating point arithmetic:
1. perform your calculations
2. convert your results to int (thereby removing the decimal digits)
3. convert to string
4. parse through the string and add commas.

steps 2 & 3 above could be done in one line:

strVal = str(int(calculatedVal))

If you used the Decimal class to perform your calculations, and your results are already in strings then:
1. Perform your calculations
2. Use split, as mentioned by djidjadji (but don't cast the first part to an int keep it as a string!)
3. parse through the resultant string and add your commas.

Cheers for now,
Jas.

To round off decimal

IDLE 2.6.2      
>>> 0.0225
0.022499999999999999
>>> round(0.0225, 3)
0.023
>>> round(0.0225, 2)
0.02

>>> a = 15.1548
>>> print '%.2f' % a
15.15

>>> print '%.3f' % a
15.155

#Look at type
>>> s = '1.458045577554545454'
>>> type(s)
<type 'str'>

 >>> x = 1.458045577554545454
>>> type(x)
<type 'float'>

>>> y = 54
>>> type(y)
<type 'int'>

#Let try round on a string
>>> s = '1.458045577554545454'
>>> s
'1.458045577554545454'
>>> round(s, 2)

Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    round(s, 2)
TypeError: a float is required

#Ok a float is required
>>> b = float(s)
>>> b
1.4580455775545456
>>> round(b, 2)
1.46

Thanks for all the replies, really appreciate it! While not all of the information was necessary for the situation, I still learned a bunch of stuff. Like using split, converting things to integers, what the 'type' command is and a bunch of other stuff. Now because I wish my program to have decimal support in the future, I believe I am going to use the split function and save the decimals to a variable, do the calculations, parse the string, add the commas, use the append function to then add the decimal back on. Again, thanks for all the help! I should have a program in no time.

This question has already been answered. Start a new discussion instead.