stevecondylios / priceR

Economics and Pricing in R
https://stevecondylios.github.io/priceR/
Other
59 stars 7 forks source link

Commercial rounding for convenience #30

Open stevecondylios opened 3 years ago

stevecondylios commented 3 years ago
round2 = function(x, decimal_places = 0) {
  posneg = sign(x)
  z = abs(x)*10^decimal_places
  z = z + 0.5 + sqrt(.Machine$double.eps)
  z = trunc(z)
  z = z/10^decimal_places
  z*posneg
}

Examples:

round2(3.5)
[1] 4

round2(2.5)
[1] 3

round2(0.15, decimal_places = 1)
[1] 0.2

Consider roundc() (or other name) so that: