Multiply numbers and return number with decimal point

I don't know if I am asking my question correctly, but I want to know if this can multiply two numbers for me and place the decimal depending on the length of the number?

I am currently doing something like this

def multiply(input1, input1):
    num = (input1 * input1)
    if len(str(num)) == 4 and str(num).isdigit():
        print(num)

    return num

multiply(225, 10)

      

Instead of returning something like 2250, I would like to return something like 2.25k. Is there a built-in function in python to do this?

Thank.

Now I am doing something like this

from __future__ import division
    def multiply(string1, string2):
        num = (string1 * string2)
        if len(str(num)) == 4 and str(num).isdigit():
            num = format(num/1000, '.2f')
            print(num + "k")
        elif len(str(num)) == 5 and str(num).isdigit():
            num = format(num/1000, '.1f')
            print(num + "k")
        return num
multiply(225, 10)

      

How effective is it?

+3


source to share


1 answer


If you want a simple example of how this can be done, take a look.



def m(a, b):
     num = float(a * b)

     if num / 1000 > 1:
         return str(num/1000) + "k"
     else:
         return num

>>> m(225, 10)
'2.25k'

      

+1


source







All Articles