Godot Version
4.6.1
So I know this discussion has come up a few different times but I couldn’t find any scripts that formatted it the way I wanted for my game and I’m super inexperienced so trying to adapt them wasn’t working. My code looks clunky to me but it works for postives and negatives up to 999 trillion. After that the formatting breaks but I could easily update it to include even larger numbers if I decide my game needs it. All it does is take the total saved as the smallest incriment available (so cents in this case) and splits it up into their respective sections. I feel like someone else could come up with better but it works and makes me happy.
extends Node
#This script converts money from cents into something readable by the player for example
#new_total(15000) would result in $15.000
@export var current_cents : int
@export var current_hundreds : int
@export var current_thousands : int
@export var current_millions : int
@export var current_billions : int
@export var current_trillions : int
@export var current_total : String
func format_currancy(new_trillions : int, new_billions : int, new_millions : int, new_thousands : int, new_hundreds : int, new_cents : int):
if new_trillions > 0:
current_total = "$" + str(new_trillions) + "," + str(new_billions).pad_zeros(3) + "," + str(new_millions).pad_zeros(3) + "," + str(new_thousands).pad_zeros(3) + "," + str(new_hundreds).pad_zeros(3) + "." + str(new_cents).pad_zeros(3)
elif new_billions > 0:
current_total = "$" + str(new_billions) + "," + str(new_millions).pad_zeros(3) + "," + str(new_thousands).pad_zeros(3) + "," + str(new_hundreds).pad_zeros(3) + "." + str(new_cents).pad_zeros(3)
elif new_millions > 0:
current_total = "$" + str(new_millions) + "," + str(new_thousands).pad_zeros(3) + "," + str(new_hundreds).pad_zeros(3) + "." + str(new_cents).pad_zeros(3)
elif new_thousands > 0:
current_total = "$" + str(new_thousands) + "," + str(new_hundreds).pad_zeros(3) + "." + str(new_cents).pad_zeros(3)
elif current_hundreds > 0:
current_total = "$" + str(new_hundreds).pad_zeros(2) + "." + str(new_cents).pad_zeros(3)
elif current_cents > 0:
current_total = "$" + str(new_hundreds).pad_zeros(2) + "." + str(new_cents).pad_zeros(3)
elif new_trillions < 0:
current_total = "-$" + str(new_trillions - (new_trillions * 2)) + "," + str(new_billions - (new_billions * 2)).pad_zeros(3) + "," + str(new_millions - (new_millions * 2)).pad_zeros(3) + "," + str(new_thousands - (new_thousands * 2)).pad_zeros(3) + "," + str(new_hundreds - (new_hundreds * 2)).pad_zeros(3) + "." + str(new_cents - (new_cents * 2)).pad_zeros(3)
elif new_billions < 0:
current_total = "-$" + str(new_billions - (new_billions * 2)) + "," + str(new_millions - (new_millions * 2)).pad_zeros(3) + "," + str(new_thousands - (new_thousands * 2)).pad_zeros(3) + "," + str(new_hundreds - (new_hundreds * 2)).pad_zeros(3) + "." + str(new_cents - (new_cents * 2)).pad_zeros(3)
elif new_millions < 0:
current_total = "-$" + str(new_millions - (new_millions * 2)) + "," + str(new_thousands - (new_thousands * 2)).pad_zeros(3) + "," + str(new_hundreds - (new_hundreds * 2)).pad_zeros(3) + "." + str(new_cents - (new_cents * 2)).pad_zeros(3)
elif new_thousands < 0:
current_total = "-$" + str(new_thousands - (new_thousands * 2)) + "," + str(new_hundreds - (new_hundreds * 2)).pad_zeros(3) + "." + str(new_cents - (new_cents * 2)).pad_zeros(3)
elif current_hundreds < 0:
current_total = "-$" + str(new_hundreds - (new_hundreds * 2)).pad_zeros(2) + "." + str(new_cents - (new_cents * 2)).pad_zeros(3)
elif current_cents < 0:
current_total = "-$" + str(new_hundreds - (new_hundreds * 2)).pad_zeros(2) + "." + str(new_cents - (new_cents * 2)).pad_zeros(3)
return current_total
func new_total(new_cents : int):
var total_length : int = len(str(new_cents))
if total_length > 3:
@warning_ignore("integer_division")
current_hundreds = new_cents / 1000
current_cents = new_cents - (current_hundreds * 1000)
var hundreds_length : int = len(str(current_hundreds))
if hundreds_length > 3:
@warning_ignore("integer_division")
current_thousands = current_hundreds / 1000
current_hundreds = current_hundreds - (current_thousands * 1000)
var thousands_length : int = len(str(current_thousands))
if thousands_length > 3:
@warning_ignore("integer_division")
current_millions = current_thousands / 1000
current_thousands = current_thousands - (current_millions * 1000)
var millions_length : int = len(str(current_millions))
if millions_length > 3:
@warning_ignore("integer_division")
current_billions = current_millions / 1000
current_millions = current_millions - (current_billions * 1000)
var billions_length : int = len(str(current_billions))
if billions_length > 3:
@warning_ignore("integer_division")
current_trillions = current_billions / 1000
current_billions = current_billions - (current_trillions * 1000)
else:
current_cents = new_cents
return format_currancy(current_trillions, current_billions, current_millions, current_thousands, current_hundreds, current_cents)