Re: [R] trunc function

From: Petr Savicky <>
Date: Wed, 09 Mar 2011 11:03:00 +0100

On Wed, Mar 09, 2011 at 05:36:35PM +0800, Luis Felipe Parra wrote:
> Hello. I would like to know if there exists in R a function like trunc but
> where i can choose how many decimal places can I stay with in the number I
> have (sort of the same as the trunc function in excel). I would like, for
> example if I have the number 0.974678 and I choose to stay with 3 decimal
> places to have as ouput 0.974.


Try the following

  trunc.dig <- function(x, digits) trunc(x*10^digits)/10^digits

  trunc.dig(0.974678, 3)

  [1] 0.974

  trunc.dig(10.974999, 3)

  [1] 10.974

Hope this helps.

Petr Savicky. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Mar 2011 - 10:06:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Mar 2011 - 11:00:20 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive