0

I'm trying to ask a user to type in a floating point number. My program should then print it out as integer and a decimal. What I'm looking for is:

If user types : 1.34 ... then integer should print: 1 and decimal should print: 0.34

Here's what I'm doing:

number = float(input('Number: '))
print('integer: ', int(number))
print('decimal: ', number / 1))

I'm oblivious as to how do I round up to get exactly 0.34. If I should convert the number to float again in line 3 or divide the original number by 100 or something.

Ron
  • 3
  • 2
  • What behavior do you want in the negatives? For `-6.1` what response would you be going for? – Eric Jin May 30 '22 at 21:39
  • 0.61 is probably what i will be looking for. don't know if math is correct, because right now i'm just testing with positive integers, not negative. it should be rounded up. sorry if this confuses you, i'm already confused myself. i don't know if there's something to do with floor function. – Ron May 30 '22 at 21:45

4 Answers4

0

Just take the integer from the original float (assuming the number is positive)

number = float(input('Number: '))
print('integer: ', int(number))
print('decimal: ', number - int(number)))

Yes sometimes the result may be slightly inaccurate. This is a consequence of using floating point numbers here's an explanation. As Eric pointed out rounding to several decimal places is a solution to this e.g. round(number - int(number), 10)

Jacob
  • 750
  • 5
  • 19
  • the answer comes out 0.34100000000001. not what i'm looking for. :( – Ron May 30 '22 at 21:41
  • That's normal. It's called floating point roundoff. If you don't want that you can try `round(number-int(number), 10)` to remove the extra precision. – Eric Jin May 30 '22 at 21:42
  • Yes, thank you this works. I also tried this way after looking at your first comment `number = float(input("number: "))` `integer = int(number)` `decimal = number - integer` `print(f"Integer part: {integer}")` `print(f"Decimal part: {decimal}")` sorry i don't know how to format code – Ron May 30 '22 at 21:53
0

dividing is clearly not the perfect solution to do that, you can use the function round as follows:

number = str(round(float(input('Number: ')), 2))
integer, dec = number.split('.')

print(f'integer: {integer}, decimal: 0.{dec}')

output:

Number: 1.2658
integer: 1, decimal: 0.27
mrCopiCat
  • 899
  • 3
  • 15
0

Consider the following:

i,d=map(int, number.split('.') if '.' in number else [number, '0'])

Covers whole numbers without forcing user to input 1.0

Jacek Błocki
  • 452
  • 3
  • 9
0

this is another way to get 2 decimal places

 number = float(input('Number: '))
    print('integer: ', int(number))
    dec= number - int(number)
    print('decimal: ', round(dec,2))
    
    Number: 1.34
    integer:  1
    decimal:  0.34
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '22 at 05:21