rurema/doctree — pull request #119 by yui-knk (2014-08-29 15:33:29+00:00)
Title: Numericのroundup, down, offの修正

[修正前の方法]

```
class Numeric
def roundup(d=0)
x = 10**d
if self > 0
(self * x).ceil.quo(x)
else
(self * x).floor.quo(x)
end
end

def rounddown(d=0)
x = 10**d
if self < 0
(self * x).ceil.quo(x)
else
(self * x).floor.quo(x)
end
end

def roundoff(d=0)
x = 10**d
if self < 0
(self * x - 0.5).ceil.quo(x)
else
(self * x + 0.5).floor.quo(x)
end
end
end

[3] pry(main)> 153.roundup(1)
=> (153/1)
[4] pry(main)> 153.roundup(2)
=> (153/1)
[5] pry(main)> 153.rounddown(1)
=> (153/1)
[6] pry(main)> 153.rounddown(2)
=> (153/1)
[7] pry(main)> 153.roundoff(1)
=> (153/1)
[8] pry(main)> 153.roundoff(2)
=> (153/1)
[9] pry(main)>
[10] pry(main)> -153.roundup(1)
=> (-153/1)
[11] pry(main)> -153.roundup(2)
=> (-153/1)
[12] pry(main)> -153.rounddown(1)
=> (-153/1)
[13] pry(main)> -153.rounddown(2)
=> (-153/1)
[14] pry(main)> -153.roundoff(1)
=> (-153/1)
[15] pry(main)> -153.roundoff(2)
=> (-153/1)
```

[修正後の方法]

```
class Numeric
def roundup(d=0)
x = 10**d
if self > 0
self.quo(x).ceil * x
else
self.quo(x).floor * x
end
end

def rounddown(d=0)
x = 10**d
if self < 0
self.quo(x).ceil * x
else
self.quo(x).floor * x
end
end

def roundoff(d=0)
x = 10**d
if self < 0
(self.quo(x) - 0.5).ceil * x
else
(self.quo(x) + 0.5).floor * x
end
end
end

[5] pry(main)> 153.roundup(1)
=> 160
[6] pry(main)> 153.roundup(2)
=> 200
[7] pry(main)> 153.rounddown(1)
=> 150
[8] pry(main)> 153.rounddown(2)
=> 100
[9] pry(main)> 153.roundoff(1)
=> 150
[10] pry(main)> 153.roundoff(2)
=> 200
[11] pry(main)>
[12] pry(main)> -153.roundup(1)
=> -160
[13] pry(main)> -153.roundup(2)
=> -200
[14] pry(main)> -153.rounddown(1)
=> -150
[15] pry(main)> -153.rounddown(2)
=> -100
[16] pry(main)> -153.roundoff(1)
=> -150
[17] pry(main)> -153.roundoff(2)
=> -200
```