Skip to content
Prev 299477 / 398503 Next

number of decimal places in a number?

On Sat, Jul 07, 2012 at 11:52:35AM +0300, Martin Ivanov wrote:
Hi.

Try the following.

  getDigits <- function(x)
  {
      out <- format.info(x, digits=10)
      stopifnot(out[3] == 0)
      out[2]
  }

The function format.info() rounds the input number
to "digits" significant digits and then outputs the
width of the field for printing, the number of digits
after the decimal dot and some information on the exponent 
(out[3] == 0, if exponent is not used). So, the required
number of digits in the fractional part is out[2].

  getDigits(3.123456)

  [1] 6

Hope this helps.

Petr Savicky.