Reputation: 737
This is pretty straight forward:
> sprintf("%013d",150025901)
[1] "0000150025901"
> sprintf("%013d",8150025901)
Error in sprintf("%013d", 8150025901) :
invalid format '%013d'; use format %f, %e, %g or %a for numeric objects
ultimately I need to use this on a 12 digit number, but I just removed digits until sprintf would stop returning that error.
Upvotes: 3
Views: 1428
Reputation: 574
8150025901
is too big for an integer, which maxes out at 2147483647
You can use sprintf with a double instead of an int and get the desired results. The exact code for this would be:
sprintf("%013.000f",8150025901)
However, it is important to note that--while R will not give you an explicit error or warning--if you try to do this with numbers more than ~15 digits, you can get unpredictable results.
This is because doubles in R have 53-bit precision, and 10^15 < 2^53 < 10^16. That means the rounding error in the number you are converting is greater than one for 16-digits, so (for example) sprintf("%013.000f",10^16)
and sprintf("%013.000f",10^16+1)
both produce "10000000000000000"
due to rounding
Upvotes: 6