0

If n = 19.879287624 then how to change it into n = 19.87 without using format, which means n remains a float and not a string. Obviously, round is irrelevant since it returns 19.88 and not 19.87. I checked other similar questions but most of answers were using format and the rest (without using format) were not clear enough.

Thanks.

plpm
  • 539
  • 3
  • 12

1 Answers1

2

You can do this:

n = 19.879287624

n = int(n*100)/100

It will give you 19.87.

I think it's pretty obvious, but in case it isn't: multiplying by 100 moves the decimal place to the right by two spaces, forcing it to be an int truncates the rest, then dividing that int by 100 moves the decimal place back where it belongs.

corvelk
  • 171
  • 7