How do I make sure I only display 1 decimal?

Get help using Construct 2

Post » Wed Sep 17, 2014 5:03 pm

I feel like I have been bothering you a lot lately, but I swear my game is almost finished now :P

Today I wanted to share about a seemingly random thing I've seen that is starting to annoy me.

I have a counter for each of my levels (Every 0.1 seconds -> Add 0.1 to Timer_X). This works like a charm.

At the end of each level I add the time to a webstorage local key (Set local key "Time_LevelX" to round(Timer_LevelX*10)/10). This will round down and save only 1 decimal to the key.

However, in an overview page, I'm displaying the total time for each level added up:

Set text to float(WebStorage.LocalValue("Time_Level1"))+float(WebStorage.LocalValue("Time_Level2")) +float(WebStorage.LocalValue("Time_Level3"))+++ and so on.

Most of the time this looks good, but once in a blue moon it will display a long row of decimals, even if the keys separately only has 1 decimal.

Any way I can improve my math?

Total_Time.png
You do not have the required permissions to view the files attached to this post.
B
14
S
3
Posts: 123
Reputation: 1,571

Post » Wed Sep 17, 2014 5:12 pm

Just do the same *10 /10 rounding on the total.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Wed Sep 17, 2014 5:41 pm

ramones wrote:Just do the same *10 /10 rounding on the total.


I am not a smart man. Not sure exactly how to write it. Currently I have played for 61.099999.. seconds, and I want to display this as 61.0 s or 61.1 s (or even better 1.00 s/1.01 s, but from what I have tried this seems like an even bigger job.

Originally - will display "61.0999999..."

float(WebStorage.LocalValue("Time_Level1"))+float(WebStorage.LocalValue("Time_Level2")) +float(WebStorage.LocalValue("Time_Level3"))+float(WebStorage.LocalValue("Time_Level4"))+float(WebStorage.LocalValue("Time_Level5"))+float(WebStorage.LocalValue("Time_Level6"))+float(WebStorage.LocalValue("Time_Level7"))+float(WebStorage.LocalValue("Time_Level8"))+float(WebStorage.LocalValue("Time_Level9"))+float(WebStorage.LocalValue("Time_Level10"))+float(WebStorage.LocalValue("Time_Level11"))+float(WebStorage.LocalValue("Time_Level12"))+float(WebStorage.LocalValue("Time_Level13"))+float(WebStorage.LocalValue("Time_Level14"))+float(WebStorage.LocalValue("Time_Level15")) & " s"

This will give me "6 s"

round((float(WebStorage.LocalValue("Time_Level1"))+float(WebStorage.LocalValue("Time_Level2")) +float(WebStorage.LocalValue("Time_Level3"))+float(WebStorage.LocalValue("Time_Level4"))+float(WebStorage.LocalValue("Time_Level5"))+float(WebStorage.LocalValue("Time_Level6"))+float(WebStorage.LocalValue("Time_Level7"))+float(WebStorage.LocalValue("Time_Level8"))+float(WebStorage.LocalValue("Time_Level9"))+float(WebStorage.LocalValue("Time_Level10"))+float(WebStorage.LocalValue("Time_Level11"))+float(WebStorage.LocalValue("Time_Level12"))+float(WebStorage.LocalValue("Time_Level13"))+float(WebStorage.LocalValue("Time_Level14"))+float(WebStorage.LocalValue("Time_Level15"))*10)/10) & " s"

And this will display "6.1099999..."

((float(WebStorage.LocalValue("Time_Level1"))+float(WebStorage.LocalValue("Time_Level2")) +float(WebStorage.LocalValue("Time_Level3"))+float(WebStorage.LocalValue("Time_Level4"))+float(WebStorage.LocalValue("Time_Level5"))+float(WebStorage.LocalValue("Time_Level6"))+float(WebStorage.LocalValue("Time_Level7"))+float(WebStorage.LocalValue("Time_Level8"))+float(WebStorage.LocalValue("Time_Level9"))+float(WebStorage.LocalValue("Time_Level10"))+float(WebStorage.LocalValue("Time_Level11"))+float(WebStorage.LocalValue("Time_Level12"))+float(WebStorage.LocalValue("Time_Level13"))+float(WebStorage.LocalValue("Time_Level14"))+float(WebStorage.LocalValue("Time_Level15"))*10)/10) & " s"
B
14
S
3
Posts: 123
Reputation: 1,571

Post » Wed Sep 17, 2014 6:03 pm

It would be easier to add up the values first and then round it.
total = float(WebStorage.....) + float(WebStorage....) + ....
set text to round(total * 10) / 10

Actually I think if you add up the values into a variable and then set the text to the variable, it gets rounded automatically.

You could even use a loop to add up the values:
sumWebstorageValues.PNG
You do not have the required permissions to view the files attached to this post.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Wed Sep 17, 2014 6:21 pm

Sweet! The looping makes it a lot easier.
Thanks a lot.
B
14
S
3
Posts: 123
Reputation: 1,571

Post » Sat Sep 27, 2014 5:38 pm

I'm sorry, but how would I do the calculations if I wanted to display 2 decimals?
B
14
S
3
Posts: 123
Reputation: 1,571

Post » Sat Sep 27, 2014 5:57 pm

Just replace the 10s with 100s.
ImageImageImage
B
71
S
21
G
231
Posts: 3,691
Reputation: 129,358

Post » Sat Sep 27, 2014 6:17 pm

Ah, thanks. I think it helps to add dt every tick as well, instead of adding 0.1 sec every 0.1 sec as I have tried.
B
14
S
3
Posts: 123
Reputation: 1,571


Return to How do I....?

Who is online

Users browsing this forum: Lordshiva1948 and 53 guests