I have a number, for example 1.128347132904321674821 that I would like to show as only two decimal places when output to screen (or written to a file). How does one do that?
for 2 decimal places assuming that you want to keep trailing zeros
sprintf(5.5, fmt = '%#.2f')
which gives
[1] "5.50"
As @mpag mentions below, it seems R can sometimes give unexpected values with this and the round method e.g. sprintf(5.5550, fmt='%#.2f') gives 5.55, not 5.56