How to split a number into two different ones?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SebSharper

I am trying to implement a system to split the items in my inventory to another slot. For example, let’s say that I have a stacked slot in which one I have 6 items, when splitting, I should have 3 on the initial slot, and 3 in a second slot.
The problem I’m facing is when the number is not an even number. If I split a slot that has 5 items, the initial one should have 3 items, and the second one 2.
How could I get the corresponding values for this?

:bust_in_silhouette: Reply From: klaas

like so … ?

var max_items_per_column:float = 3
var column_index = floor( item_index / max_items_per_column )
:bust_in_silhouette: Reply From: jgodfrey

So, you’re just trying to calculate the number of items to place into 2 separate columns based on the total number of items? And, with an even number of items, each column should have the same number of items and with an odd number of items, the left-hand column should have 1 more?

If so, something like this should work:

func _ready():
	var total_items = 11
	var left = ceil(float(total_items) / 2)
	var right = total_items - left
:bust_in_silhouette: Reply From: kufyweruk
func split(val):
	var valInSlot1=(val/2) as int
	var valInSlot2=val-valInSlot1