R round to nearest .5 or .1

后端 未结 4 455
忘了有多久
忘了有多久 2020-11-27 15:44

I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56). I am now trying to round to a specific value which is different

4条回答
  •  渐次进展
    2020-11-27 16:06

    I'm not familiar with R the language, but my method should work with any language with a ceiling function. I assume it's rounded UP to nearest 0.5:

    a = ceiling(a*2) / 2
    
    if a = 0.4, a = ceiling(0.4*2)/2 = ceiling(0.8)/2 = 1/2 = 0.5
    if a = 0.9, a = ceiling(0.9*2)/2 = ceiling(1.8)/2 = 2/2 = 1
    

提交回复
热议问题