Maths: Split

Get help using Construct 2

Post » Thu Oct 06, 2011 10:09 pm

Hello, Dcrew here!

I wondered how I can split a string, so I can retrieve for example from this integer:

495825542

To get 1-3 # from the Int: 495
To get 4-6 # from the Int: 825
To get 7-9 # from the Int: 542

I want to achieve this so I can display this integer in a string like so:
$ 495,825,542

So it looks more readable, unless you have an alternative answer :)dcrew2011-10-06 22:14:28
B
17
S
4
G
5
Posts: 226
Reputation: 4,042

Post » Thu Oct 06, 2011 11:04 pm

http://dl.dropbox.com/u/1013446/moneytext.capx
let me know if you need me to deconstruct itlucid2011-10-06 23:04:59
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Thu Oct 06, 2011 11:17 pm

I was looking to use something like this for a similar problem... but I definitely need you to deconstruct it.
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Thu Oct 06, 2011 11:56 pm

ok
apparently c2 doesn't allow very large numbers, because it kept rounding when I'd use a number global instead of text and convert it to a string.

so first we set our global text variable to the number

the we do a loop from 1 to len(number)/3

len(number) is the length of the string,(called "number") as in, how many characters in the string divided by 3, because we want it in chunks of 3 for the comma
xxx,xxx,xxx,xxx,etc

then we have mid(number,len(number)-loopindex*3,3)&((loopindex>1)?",":"")&text.text

this we will break down piece by piece, first we have:
mid(Text, Index, Length)
this will get you a substring of Text, beginning at character Index, for Length characters, so if you did
mid("This string here", 3, 7)
you would get
"s strin"
you started at the index 3 in the string (0,1,2,3), and took 7 letters with you

so we have
mid(number,len(number)-loopindex*3,3)
our string is called "number" (to avoid confusion)
number = "495825542"
so we have mid("495825542",len("495825542")-loopindex*3,3)

len(number) returns the number of letters in the string
in this case 9, remember this parameter we're working on is the Index letter we want the substring to start on (   mid(Text, Index, Length) )
so len("495825542") has 9 letters, so in this case it equals 9
now we have
mid("495825542",9-loopindex*3,3)
loopindex*3 will start us on multiples of 3, but since we are minusing from 9 it will go from right to left:
loopindex=1
9-(1*3) = 6
so we start on index 6 of "495825542"
so we're on the last digit '5'
   "495825(5)42"
and we are taking 3 characters "542"

next loopindex would be:
9-(2*3) = 3
so "495(8)25542"
and we take those 3 characters "825"
etc

we go from right to left, because if we have a number that's not divisible by 3, we don't want to have this
678,45
instead of
67,845

but the entire expression was:
mid(number,len(number)-loopindex*3,3)&((loopindex>1)?",":"")&text.text

so we have the mid() out of the way
then we have &((loopindex>1)?",":"")
this is a conditional operator:

condition?if true:if false

for instance if you did
5>4?"true":"false"
that expression would evaluate to "true" because the condition was true

if you did
5<4?"good":"bad"
if would evaluate to "bad" because 5 is not less than 4

in our expression:
((loopindex>1)?",":"")
we check if loopindex is equal to 1 and if it is not we add a "," to our string, and if it is we add "" (nothing) to our string
this is because once again we are working from right to left, and we don't want our numbers looking like this:
100,000,   (with the extra comma at the end)
instead of just
100,000

one more look at our expression:
mid(number,len(number)-loopindex*3,3)&((loopindex>1)?",":"")&text.text
at the end we have &text.text
since we are doing the right side first we can't append to the end of the string
we have to add to the beginning, so we are getting our three numbers &text.text to add what we already have to the right side of our our current expression

after all that is over
we do:
"$"&text.text
to add the dollarsign to the left of our current text

make sense?

EDIT: i just tested this with nondivisible by 3 numberlengths and it did not work correctly. so I reuploaded and it has one extra step:
if (len(number)%3>0)
% operator gives the remainder of a division problem
like 9%3=0 because 9/3=3 with no remainder
8%3=2 because 8/3=2 r2 (a remainder of two)
so we want to know if the number of digits is divisible by 3
if it is, there is no remainder, we don't want an extra comma in there, so we skip this step, if it is greater than 0 there are some digits leftover, but less than three, actually there are exactly len(number)%3 digits left over so we :
set text to:
left(number,len(number)%3)&","&text.text
left is like mid but from the left side of the string
left(Text, Count)

so our Text is number
and the number of characters we want(Count) is len(number)%3 because that's how many characters are leftover after our clever little *3 loop.
then we add our "," and connect it to the rest of our number


lucid2011-10-07 00:17:19
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Fri Oct 07, 2011 12:07 am

Dam i have headache..... tonycrew2011-10-07 00:07:44
B
40
S
14
G
11
Posts: 243
Reputation: 9,427

Post » Fri Oct 07, 2011 12:57 am



Um.

Maybe what I wanted to modify it for can use a simpler solution... I just need to be able to go through a number and tell what each digit is individually. Is that as complicated to do?
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Fri Oct 07, 2011 1:04 am

[QUOTE=SullyTheStrange]

Um.

Maybe what I wanted to modify it for can use a simpler solution... I just need to be able to go through a number and tell what each digit is individually. Is that as complicated to do?[/QUOTE]

make sure it's a string and then just do mid(stringhere,index of character you want,1)

because:
mid(Text, Index, Length)
your Text is your number in string form, the Index is what digit you want and the Length is 1 since you just want that 1 digitlucid2011-10-07 01:08:16
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Fri Oct 07, 2011 1:31 am

Alrighty, I've basically got it working, except for one thing... What I'm doing is related to the score in my game. If the score is held in a string, is there any way to add to it? I was going to put it in a number variable and copy it over, but you're right, C2 can't have very large numbers. Which makes having the typical game style of very large scores... impossible. Is there any way around this, or do I have to wait until they change that rounding thing?
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Fri Oct 07, 2011 7:31 am

And what do you think of that ?

bigNumberDisplay.capx

Just store units, thousand, million, billion in different variables
Increment each one depending on the preceding one
and just use a bit of string manip to zero fill them when displayed.

right("000"&number,3) does the trick.

:D Are you appy?Yann2014-01-27 09:44:09
Last edited by Yann on Sun Sep 14, 2014 11:32 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Oct 07, 2011 7:35 am

Sadly I am in the office and can't create a capx by now .. so I will just type my thoughts =)

I am not even nearly as good as lucid, but I would try to solve the problem more like this:

Crating 9 sprites with 10 animation-frames (digits 0-9) and giving them variables, so if you score 2341 points, you will change the first 4 digits by 2, 3, 4 and 1 .. than a little calculation (if var > 10: set var-10 and add 1 to var2) and so on you will have a possibility to have a score as big as you want.

Or am I talking useless garbage? =/
B
17
S
4
G
4
Posts: 94
Reputation: 3,058

Next

Return to How do I....?

Who is online

Users browsing this forum: plinkie and 15 guests