-1

In the same function, I have tried to use integer, float, and rounding, but I could not get this result. What did I do wrong?

The goal is:

10*12.3 = 123

3*12.3= 36.9

my code:

def multi(n1, n2):
    x = n1*n2
    return x

I have tried int(n1*n2), but I got 123 and 36. Then I tried float(n1*n2) and I got 123.0 and 36.9. What did I do wrong and how can I fix it?

Filip
  • 898
  • 1
  • 8
  • 24
Alice
  • 3
  • 3
  • You are not doing anything wrong. If you multiply a float by another number (int or float) the result will always be a float. – Selcuk Mar 19 '20 at 08:09

2 Answers2

1

You are always multiplying an integer with a float which will always output a float.

If you want the number that your function returns to be a float with 1 decimal point you can use round(num, 1).

def multi(n1, n2):
    x = n1*n2
    return round(x, 1)

print(multi(10, 12.3))  # outputs '123.0'
print(multi(3, 12.3))  # outputs '36.9'

To escape the .0 you could probably use an if statement although I don't see the use of it, since doing calculations with floats have the same output as integers (when they are .0)

def multi(n1, n2):
    x = n1 * n2
    return round(x, 1)

output = []
output.append(multi(10, 12.3))  # outputs '123.0'
output.append(multi(3, 12.3))  # outputs '36.9'

for index, item in enumerate(output):
    if int(item) == float(item):
        output[index] = int(item)

print(output)  # prints [129, 36.9]

This should probably help you but it shouldn't matter all that match to you

Filip
  • 898
  • 1
  • 8
  • 24
0

The number is not the representation of the number. For example, all these representations are 123:

123
12.3E1
123.0
123.0000000000000000000

My advice is to do them as floating point and either use output formatting to get them all in a consistent format:

>>> for i in (12.3 * 10, 42., 36.9 / 10):
...     print(f"{i:8.2f}")
...
  123.00
   42.00
    3.69

or string manipulation to remove useless suffixes:

>>> import re
>>> x = 12.3 * 10
>>> print(x)
123.0
>>> print(re.sub(r"\.0*$", "", str(x)))
123
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I just got some wierd bug where multiplying np.int64 with np.uint64 resulted in a float (?!), which in turn was no longer suited for indexing. If what you say is true, would you care to elaborate why does then the indexing require integer values and can't be done with rounded floats? – shcrela Apr 28 '23 at 13:11
  • related: https://github.com/numpy/numpy/issues/19294 – shcrela Apr 28 '23 at 13:25
  • 1
    @shcrela, that seems rather tangential to the info given here (which involves *output* rather than use when indexing). Python *can* index with rounded floats (as integers) but it won't do that for you implicitly. You need to coerce the type into an integer with something like `int(x)`. – paxdiablo Apr 28 '23 at 18:37
  • That was kind of my point (just nagging about the oddities of the world). Despite the fact that 123 and 123.0 are in the essence the same number, their representation (int vs float) matters i.e. makes them more or less suitable for stuff like indexing. On the other side, the int 1 and the boolean True are indistinguishable for all intents and purposes even though they conceptually differ more (at least in my mind). – shcrela May 17 '23 at 07:07