1

The f_num function from the numform package will remove leading zeros from a number:

f_num(0.1)

Output:

.1

I need this very same thing, but with a comma instad of the period. It would also be great if the functionality of the f_num function which allows you to round up the number of decimals would be kept.

J. Doe
  • 1,544
  • 1
  • 11
  • 26

1 Answers1

2

Here is a custom alternative(see note below):

detrail <- function(num,round_dec=NULL){

   if(!is.null(round_dec)){
    num<-round(num,round_dec)
   }
   gsub("^\\d\\.",",",num)

 }
 detrail(0.1)
[1] ",1"
 detrail(1.1)
[1] ",1"

detrail(0.276,2)
[1] ",28"

NOTE:

  • To read this as numeric, you'll need to change options(OutDec) to , instead of . ie options(OutDec= ","). I have not done this as I do not like changing global options.See Also
  • This also removes any number that is not zero. Disable this by using 0 instead of \\d.
NelsonGon
  • 13,015
  • 7
  • 27
  • 57