Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

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

Thanks.

1 Answer

0 votes
by (36.8k points)

You can do this:

n = 19.879287624

n = int(n*100)/100

You will get 19.87.

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

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch.

Related questions

0 votes
1 answer
asked Apr 14, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...