summaryrefslogtreecommitdiffstats
path: root/modules/base
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-03-14 19:34:59 -0400
committerUnknown <joe2010xtmf@163.com>2014-03-14 19:34:59 -0400
commitd50e18ab6dd3de96c4d48816ab53e8ba9b74cf17 (patch)
tree7c50333d18738ea61dad8d5177989110d31cefa7 /modules/base
parent4470192c70a2b3f9b10065754731a266fd5ce721 (diff)
downloadgitea-d50e18ab6dd3de96c4d48816ab53e8ba9b74cf17.tar.gz
gitea-d50e18ab6dd3de96c4d48816ab53e8ba9b74cf17.zip
Clean code
Diffstat (limited to 'modules/base')
-rw-r--r--modules/base/tool.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 445833cebc..c7b907bee1 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -78,3 +78,53 @@ func TimeSince(then time.Time) string {
}
return then.String()
}
+
+// Subtract deals with subtraction of all types of number.
+func Subtract(left interface{}, right interface{}) interface{} {
+ var rleft, rright int64
+ var fleft, fright float64
+ var isInt bool = true
+ switch left.(type) {
+ case int:
+ rleft = int64(left.(int))
+ case int8:
+ rleft = int64(left.(int8))
+ case int16:
+ rleft = int64(left.(int16))
+ case int32:
+ rleft = int64(left.(int32))
+ case int64:
+ rleft = left.(int64)
+ case float32:
+ fleft = float64(left.(float32))
+ isInt = false
+ case float64:
+ fleft = left.(float64)
+ isInt = false
+ }
+
+ switch right.(type) {
+ case int:
+ rright = int64(right.(int))
+ case int8:
+ rright = int64(right.(int8))
+ case int16:
+ rright = int64(right.(int16))
+ case int32:
+ rright = int64(right.(int32))
+ case int64:
+ rright = right.(int64)
+ case float32:
+ fright = float64(left.(float32))
+ isInt = false
+ case float64:
+ fleft = left.(float64)
+ isInt = false
+ }
+
+ if isInt {
+ return rleft - rright
+ } else {
+ return fleft + float64(rleft) - (fright + float64(rright))
+ }
+}