Skip to content
Advertisement

How to copy R’s print(x, digits = n) in Python?

In R print(x, digits=n) does something a little complicated.

It always shows all the digits to the left of the decimal point and then, if there are any digits to the right it rounds the whole number so that at most n digits are shown.

Examples:

JavaScript

How would you do that in Python?

You can try these examples online at https://rdrr.io/snippets/

Advertisement

Answer

Here is an actual solution to the problem, although it requires some string operations, it should work

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement