Search code examples
rprintfroundingnegative-number

sprintf adds minus sign to zero after rounding


Let's do some rounding

> round(-0.001, 2)
[1] 0

I receive zero.

Now in combination with sprintf

> sprintf("%f", round(-0.001,2))
[1] "-0.000000"

Why is there a minus sign? I expected 0.000000.

$R --version
R version 2.13.1 (2011-07-08)

Solution

  • It's the nature of floating point numbers (IEEE 754), there is a -0 defined. See -0.