Commas in money?

Is there a simple way to put commas in money stats? I’ve tried to do some digging but I’m possibly searching wrong. I have a few games I’m working on dealing with large sums of money.

1 Like

Behold: Adding commas to numbers

7 Likes

Thanks so much :pray:

@spoano Because I just can’t help myself and I am procrastinating from actual work, I have had a go at building a loop that is simpler than the proposed solutions in the linked thread and will handle an infinitely long digit.

I’ve run it on every number up to 1 trillion and it seems OK. The long number in the code below prints correctly too.

*create money 0
*create display_money ""
*create current_char 0
*create char_count 1
*create length 0
*create comma_check 0

*set money 100000000000000000000000000000000000
*set length length(money)
*set current_char length

*label start_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_money "," & display_money          
    *set display_money (money#{current_char}) & display_money
    *set current_char - 1     
    *set char_count + 1
    *goto start_loop
      
You have ${display_money} money
11 Likes

Thanks! This is amazing!

3 Likes

Thanks, I will see which way works best for me. My coding can be a bit sloppy compared to other’s, but what ever works best for you to understand and get the wanted results right?

1 Like

The linked thread mentioned that the offered solutions wouldn’t handle decimal points, and nor would my above solution.
I figured I would add that functionality to my code and post it here so that it’s here alongside the basic code, in case anyone finds it useful.

The addition here is that it first parses each character inside the ‘money’ variable and checks each one to see if it is a ‘.’ - it is assuming the value is either an integer or a decimal, it won’t work on something with multiple ‘.’

If it doesn’t find a full stop then it just continues down the original path and just adds in the commas. Otherwise, it records the location of the full stop and initiates a second loop. This next loop simply adds every character that is to the right of the full stop (including the full stop itself) to our eventual value.

So, if we had a value of 1234.5678
It would find the full stop at character 5 and then add characters 5,6,7,8 and 9 to our ‘display money’ variable as ‘.5678’ - as we don’t add commas after the decimal point, we can just add these characters to our number as they are.

Then it goes to the original loop to add the commas. Here we re-calculate the length of the number to only include all the numbers before the decimal point and parse those characters as before, adding commas as we go.

So we end up with 1,234.5678

I’ve tested it a fair bit, it should all work fine!

*create money 0
*create display_money ""
*create current_char 0
*create char_count 1
*create length 0
*create comma_check 0

*create decimal_current_char 0
*create decimal_check ""
*create decimal_char_loc ""

*set money 1000000.10000
*set length length(money)
*set current_char length
*set decimal_current_char length

*label decimal_loop
*if decimal_current_char > 0
    *set decimal_check money#{decimal_current_char}
    *if decimal_check = "."
        *set decimal_char_loc decimal_current_char
        *goto decimal_add_loop
    *set decimal_current_char - 1
    *goto decimal_loop
*goto comma_loop

*label comma_loop_decimal_alteration
*set length decimal_char_loc - 1
*set current_char length
*label comma_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_money "," & display_money          
    *set display_money (money#{current_char}) & display_money
    *set current_char - 1     
    *set char_count + 1
    *goto comma_loop
      
You have ${display_money} money
*finish

*label decimal_add_loop
*if decimal_current_char <= length
    *set display_money display_money & (money#{decimal_current_char})
    *set decimal_current_char + 1
    *goto decimal_add_loop
*goto comma_loop_decimal_alteration
4 Likes

Nice.

1 Like

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.