0

In python, I am using rounding to 1 decimal. But If a number is 0.03 or 0.005, then It should show until the last number in the decimal places.

def calculate_total(number):
   # some number calcualtions
   number = round(number, 1)
   print(number)
   

calculate_total(66.36) # 66.4
calculate_total(3.34) # 3.3
calculate_total(3.34) # 3.3
calculate_total(0.03) # 0.0 (But it should show 0.03)
calculate_total(0.0364) # 0.0 (But it should show 0.04)
0

2 Answers 2

3

That is because you are rounding the float to one decimal place in z = 0.03 and z = 0.0364

Just change print(round(z, 1)) to print(round(z, 2)) ; this will change the decimal place from 1 to 2 and it will produce your required output.

Sign up to request clarification or add additional context in comments.

2 Comments

My early question was misleading. Sorry. I updated my question now.
Still the answer of the question remains the same.
0

You can use this type of code, providing your own condition.

In the following code, a rounding place is different depending on whether z variable is one of the value of (0.03, 0.005) or not.

z = 66.36
result = round(z, 2)

l = (0.03, 0.005)
if result in l:
    print(round(result, 1))
else:
    print(round(result, 2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.