0

I have a float number combination (min_number, max_number),and I am expecting the following round up:

(-19.12, 34.45) "rounded to" (-19.2, 34.5)
(-0.34, -0.22) "rounded to" (-0.4, -0.3)
(-0.58, -0.87) "rounded to" (-0.6, -0.9)
(-0.24, 5.98) "rounded to" (-0.3, 6.0)
(2.57, 22.38) "rounded to" (2.6, 22.4)

Note that only 1 decimal place is required. And then:

size = int(input('please input: '))
number = (min_number + max_number) / size

Then "number" will need to be rounded as well, also require 1 decimal place:

10.233333 "rounded to" 10.3
5.744542267888 "rounded to" 5.8

Any thoughts? Many thanks,

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Cook
  • 333
  • 1
  • 3
  • 8
  • 1
    Does this answer your question? [How to round away from 0 in Python 3.x?](https://stackoverflow.com/questions/51689594/how-to-round-away-from-0-in-python-3-x) – 9769953 Feb 07 '21 at 23:46

1 Answers1

0

Just multiply by 10, round and divide by 10

import math
num = input("Round a number:")
num = num.split()
print( math.ceil( float(num[0]) * 10 ) / 10,  math.ceil( float(num[1]) * 10 ) / 10 )

Note: As @0 0 pointed out this will not work for negative numbers if you want to round negative numbers then you will need to add an if statement for them and use math.floor

Yash
  • 391
  • 3
  • 14
  • Thanks Yash, I still have issue, can you see my latest post? – Cook Feb 08 '21 at 02:12
  • Ok I'll check it out – Yash Feb 08 '21 at 03:01
  • Why `num[0]`? That will just take the first character of the input string, so if the input is `12.34`, it will just use `1`. If you are instead referring to the tuples as in the question, then the input should be changed. – 9769953 Feb 08 '21 at 10:49
  • 1
    Note that this does not round away from zero for negative numbers. Using the very first example from the question, `-19.12`, `math.ceil( float(-19.12) * 10 ) / 10` results in `-19.1`, not `-19.2`. – 9769953 Feb 08 '21 at 10:52
  • I was using the tuple as an input – Yash Feb 08 '21 at 15:13
  • Any input you'd use would be a string (that is, `num` would be a string); how would you input tuples then? – 9769953 Feb 08 '21 at 16:05
  • @00 Sorry fixed it – Yash Feb 08 '21 at 16:12