56

The common code in R for rounding a number to say 2 decimal points is:

> a = 14.1234 
> round(a, digits=2)
> a
> 14.12

However if the number has zeros as the first two decimal digits, R suppresses zeros in display:

> a = 14.0034
> round(a, digits=2)
> a
> 14

How can we make R to show first decimal digits even when they are zeros? I especially need this in plots. I've searched here and some people have suggested using options(digits=2), but this makes R to have a weird behavior.

Jaap
  • 81,064
  • 34
  • 182
  • 193
M. Er
  • 881
  • 1
  • 7
  • 13
  • 9
    The code `format(round(a), nsmall = 2)` is not sufficient and it should be `format(round(a, 2), nsmall = 2)`. Since `round(a)` rounds all decimal digits, but `round(a, 2)` rounds up to 2 decimal digits. Try `a=1.987` for example. – M. Er Feb 08 '17 at 06:50

4 Answers4

81

We can use format

format(round(a), nsmall = 2)
#[1] "14.00"

As @arvi1000 mentioned in the comments, we may need to specify the digits in round

format(round(a, digits=2), nsmall = 2) 

data

a <- 14.0034
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 11
    Actually, I think this should be `format(round(a, digits=2), nsmall = 2)`, because OP wants to preserve any zeros after the decimal point when correctly rounded to 2 places, not round to 0 places and then pad with two trailing zeros. Compare: `format(round(14.0123), nsmall = 2)` (wrong) and `format(round(14.0123,digits = 2), nsmall = 2)` – arvi1000 Feb 08 '17 at 15:27
  • 1
    oh, as the OP already pointed out in a comment on the question above. You may want to edit, esp since it's the accepted answer – arvi1000 Feb 08 '17 at 15:28
  • @arvi1000 Thanks for the suggestion. I almost forgot about the `round` part – akrun Feb 08 '17 at 16:55
15

Try this:

a = 14.0034 
sprintf('%.2f',a) # 2 digits after decimal
# [1] "14.00"
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
  • 6
    Just `sprintf('%.2f', a)` will do the rounding too I believe. `sprintf('%.2f', 14.009)` gives `"14.01"` for instance. – thelatemail Feb 08 '17 at 05:49
4

The formatC function works nicely if you apply it to the vector after rounding. Here the inner function round rounds to two decimal places then the outer function formatC formats to the same number of decimal places as the number were rounded to. This essentially re-adds zeros to the number that would otherwise end without the decimal places (e.g., 14.0034 is rounded to 14, which becomes 14.00).

a=c(14.0034, 14.0056) 
formatC(round(a,2),2,format="f")
#[1] "14.00", "14.01"
Tanner33
  • 120
  • 2
  • 15
Stephen
  • 473
  • 4
  • 11
  • 6
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Jan 22 '18 at 01:43
  • for full information on formatC, one can type ?formatC – Stephen Jan 22 '18 at 16:17
-2

You can use this function instead of round and just use it like you use round function.

import decimal

def printf(x, n):
    d = decimal.Decimal(str(x))
    d0 = -(d.as_tuple().exponent)
    if d0 < n:
        print("x = ", x)
    else:
        d1 = decimal.Decimal(str(round(x, n)))
        d2 = d1.as_tuple().exponent
        MAX = n + d2
        if MAX == 0:
            print("x = ", round(x, n))
        else:
            i = 0
            print("x = ", round(x, n), end = '')
            while i != MAX:
                if i == (MAX - 1):
                    print("0")
                else:
                    print("0", end = '')
                i = i + 1

So you must have something like this.

>>> printf(0.500000000000001, 13)
>>> 0.5000000000000