Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 461
woobert Avatar asked Feb 24 '23 09:02

woobert


1 Answers

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

like image 97
Femaref Avatar answered Feb 26 '23 22:02

Femaref