Created
May 2, 2016 16:41
-
-
Save YeOldeDM/4fbe558f14a2a940afbb1cc5402a44a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
## solution for dealing with Big Friggin Numbers | |
#value is stored as a list of ints 0-999 | |
#if an int > 999, remainder is carried over to a new int | |
# 10 = [10] | |
# 100 = [100] | |
# 1000 = [0,1] | |
# 1234 = [234,1] | |
# 100,000 = [0,100] | |
# 1,000,000 = [0,0,1] | |
# 314,159 = [159,314] | |
class BigNumber: | |
var value = IntArray([0]) | |
#(this should really be somewhere else) | |
var suffix = ['','K','M','B','T','q', 'Q', 's', 'S', 'O', 'N', 'd', 'U', 'D', '!', '@', '#', '$', '%', '^', '&', '*', '**'] | |
#init the instance with a starting value | |
func _init(value=[0]): | |
self.value = value | |
#return TRUE if V < this value | |
func can_afford(V): | |
if V.value.size() > self.value.size(): | |
return false | |
elif V.value.size() == self.value.size(): | |
if V.value[V.value.size()-1] > self.value[self.value.size()-1]: | |
return false | |
return true | |
#add the value of another BigNumber to this one | |
func add_value(V): | |
var v = V | |
if V extends Value.BigNumber: | |
v = V.value | |
print("Adding "+V.get_display_value()+" to "+self.get_display_value()) | |
for i in range(v.size()): | |
_add(i,v[i]) | |
#subtract the value of another BigNumber from this | |
#(assumes the difference is >= 0) | |
func spend_value(V): | |
var v = V | |
if V extends Value.BigNumber: | |
v = V.value | |
print("Subtracting "+V.get_display_value()+" from "+self.get_display_value()) | |
for i in range(v.size()): | |
_sub(i,v[i]) | |
#print(self.value) | |
func _add(P,V): | |
#add value V to digit P | |
var diff = P+1 - self.value.size() | |
if diff > 0: | |
for i in range(diff): | |
if i == diff-1: | |
self.value.append(V) | |
else: | |
self.value.append(0) | |
else: | |
var new_value = self.value[P] + V | |
if new_value >= 1000: | |
var A = int(new_value / 1000) | |
self.value[P] = new_value - (1000*A) | |
_add(P+1,A) | |
else: | |
self.value[P] = new_value | |
func _sub(P,V): | |
#subtract value V from digit P | |
var new_value = self.value[P] - V | |
if new_value < 0: | |
self.value[P] = 1000+new_value | |
_sub(P+1,max(1,new_value/1000)) | |
else: | |
self.value[P] = new_value | |
#return a string displaying the full number value (123,456,789) | |
func get_full_display_value(): | |
var TXT = "" | |
var len = self.value.size()-1 | |
while len >= 0: | |
var S = str(self.value[len]) | |
if S.length() < 3 && len != self.value.size()-1: | |
for i in range(3-S.length()): | |
TXT += "0" | |
TXT += S | |
if len != 0: | |
TXT += "," | |
len -= 1 | |
return TXT | |
#return a string displaying short-hand number value (123.45M) | |
func get_display_value(): | |
var first = str(self.value[self.value.size()-1]) | |
var second = "" | |
var TXT1 = first | |
var TXT2 = "" | |
if self.value.size() > 1: | |
TXT1 += '.' | |
second = str(self.value[self.value.size()-2]) | |
if second.length() < 3: | |
for i in range(3-second.length()): | |
TXT2 += "0" | |
TXT2 += second | |
return TXT1+TXT2.left(2)+self.suffix[self.value.size()-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment