-2

I am trying to round up 5.9999998 to 5.999.

But I have a problem, If I do round(number) it'll round it up to 6. How can I round a number like this to max 3 decimals?

er21r21
  • 1
  • 3
  • 2
    Read (study) both answers: https://stackoverflow.com/questions/13479163/round-float-to-x-decimals – PM 77-1 Dec 01 '22 at 19:14
  • Does this answer your question? [Round float to x decimals?](https://stackoverflow.com/questions/13479163/round-float-to-x-decimals) – President James K. Polk Dec 01 '22 at 19:35
  • 2
    that isn't rounding, that is *truncating* – juanpa.arrivillaga Dec 01 '22 at 20:13
  • Are you trying to _round_, _round up_ or _truncate_? You mention all three options and it is not clear what you're asking. Please [edit] your question to clarify what you mean, and use the correct words (round/ceil/floor/truncate) to make this question searchable and useful for future readers. – wovano Dec 28 '22 at 16:36
  • Note that due to ambiguity of the question, there are now different answers with different results... – wovano Dec 28 '22 at 16:38

2 Answers2

3

You can use this:

number = 5.999999998
new_number = int(number * 1e3) / 1e3
3dSpatialUser
  • 2,034
  • 1
  • 9
  • 18
0

Here is a short code.


x = 4/3

# round up to 3 decimal places
x = round(x, 3)

print(x)