# How do I get a float from user and returns the integer portion?

I want to create a bit program that prompt the user to enter a float number and returns the integer portion of that float number,

a = input("please enter a float number: ")
print(int(a))

Hi @ljin8698 welcome to the community.

I’m just going to edit your post slightly so that the topic title is a question. I’ve also moved it into Python.

Can you please provide a link to your Repl so that others in the community can assist you.

Also if you can explain a little more around the problem you are trying to solve it would be really useful. For example, at the moment, it sounds like the sort of problem you are set in a coding class but that’s probably because we don’t know the context of the problem yet…

1 Like

what i would do i once i have the input i would use the round function to round to the nearest whole number like the piece below

``````Decimal = round(1.9)
``````

but that would return 2 if you wanted to return the actual number then maybe this would help you

``````"""
Declare the decimal
.find function only work on strings so make it a string
"""
decimal = "1.98433"#you can replace this with a input function
"""
all decimal/floats have a period
so you just need to locate it
"""
index = decimal.find(".")#will return index at which period is located
"""
Once you have located the period
you need to print everything before it
"""
number = decimal[0:index]
integer = int(number)#now just convert to an integer and you gotit
print(decimal,"as a number is",number)
"""
the output would look like this
1.98433 as a number is 1
"""
``````

Hope this helps @ljin8698 if not i can help you find a different solution

1 Like

To make the input a float, you can use the `float()` function:

``````a = float(input("please enter a float number: "))
print(int(a))
``````
1 Like

Actually it is not nesserary
Python is unlike C the variable can be any type when you change the value in it, so even without the float() it can still be output as str or float according to how your use it there, as the int(a) it give a as a float and integer it.

2 Likes

@ljin8698 can you reply to the responses or mark this as solved?

2 Likes

My way seem over complicated lol

2 Likes

Well, it kind of is. Lol

1 Like

This topic was automatically closed after 6 days. New replies are no longer allowed.