0

I have a number 120 and I divided by 100. I am getting 1.2 but actually I want to get 2 only (not 2.0) in python without importing any lib.

Can any one help here???

Mayur Koshti
  • 1,794
  • 15
  • 20
  • This is not quite a duplicate of http://stackoverflow.com/q/14822184/270986, but it's answered by the answers to that question. – Mark Dickinson Mar 05 '16 at 11:32

2 Answers2

3

You can use the modulo operator to know whether to round up the result:

120//100 + (1 if 120%100 else 0)
mhawke
  • 84,695
  • 9
  • 117
  • 138
2

You can use the built-in function ceil() to round 1.2 up to 2, and the built-in int function to cast it to an integer:

In [3]: int(ceil(120/100.0))
Out[3]: 2
gtlambert
  • 11,711
  • 2
  • 30
  • 48
  • 3
    `ceil` is actually [`math.ceil`](https://docs.python.org/3.0/library/math.html#math.ceil), so you would have to `import math` – khelwood Mar 04 '16 at 11:40
  • Interesting - I can't work out why this works in my IDE (Enthought Canopy). I haven't imported `math`..... – gtlambert Mar 04 '16 at 11:42
  • @gtlambert: If you're working in the IPython console inside Canopy, then you're using PyLab mode, in which many array-handling and plotting primitives have already been preloaded. (And in that case, your `ceil` is actually `numpy.ceil` rather than `math.ceil`.) – Mark Dickinson Mar 05 '16 at 11:01