Utility of this function - Budget App Project

Hi everyone.

I don’t know if I’m ignoring the obvious, but I personally don’t understand what is this function for. It multiplies a number by 10, but just after divides the number by 10 again, returning the initial number.

def truncate(n):
    multiplier = 10
    return int(n * multiplier) / multiplier

def getTotals(categories):
    total = 0
    breakdown = []
    for category in categories:
        total += category.get_withdrawls()
    rounded = list(map(lambda x: truncate(x/total), breakdown))
    return rounded

Question: What Truncate function is for?

Thank you.

No, I’m trying to understand someone else code.

Yes, of course. I’m stuck so I was looking for alternatives, but I didn’t understand this particular code, so I was curious about what It does.

If something doesn’t need elaborate setup the best way to check what it is doing is trying it yourself with few examples.

Having said that, there are better ways to do, in python, what this function is achieving. Person writing this code must have been used to different language.

1 Like

I strongly recommend asking questions about your code instead of looking up other people’s solutions.

Hi, thank you for your answer : ).

Thank you for your recomendation : ).

I’m writing my own code, I just wanted to understand what is the utility of this function, not even what it does because I think I understand it, but the purpose.

The function is only useful if you want to copy their approach. You shouldn’t copy other people’s approaches. You should write your own.

There is always time to look up other people’s solutions after you have written your own.

Thank you, you are right, I’ll understand this code later.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.