How to show invert a decimal number in python?
n = 12.789
i want to show it like that.
print (n)
987.21
output:987.21
How to show invert a decimal number in python?
n = 12.789
i want to show it like that.
print (n)
987.21
output:987.21
Break the problem up into its necessary steps, then figure out each one, and then combine all the steps at the end.
n
to a string (see here for 10 examples of how to do this)An example (there are many ways to do it though):
n = 12.345
n_string = str(n)
n_string_reversed = ''.join(reversed(n_string))
reversed_n = float(n_string_reversed)
Output of print(reversed_n)
:
543.21
Or, you could do all the steps on one line, but it's harder to read that way - you should generally go for readability over compactness, since code's main purpose is for humans to read, not just machines. If this were production code, it would need a big comment block explaining everything, but breaking it up into steps makes it obvious what's going on:
reversed_n = float(''.join(reversed(str(n))))
There are many other ways to accomplish this. The main goal of my answer is to encourage you to break up the problem into steps, and then figure out each one, rather than trying to go straight to the end result. Think about how you'd do the problem on paper, then figure out how to do each of those steps in code. I didn't know how to solve the problem, but I broke it up into steps, googled each of those steps, found the articles I linked, and then combined that knowledge to provide my answer.