How do I make sure I only display 1 decimal?

0 favourites
  • 8 posts
  • I feel like I have been bothering you a lot lately, but I swear my game is almost finished now

    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?

    [attachment=0:idu2zwtj][/attachment:idu2zwtj]

  • Just do the same *10 /10 rounding on the total.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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"

  • 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:

    [attachment=0:2ay32aq3][/attachment:2ay32aq3]

  • Sweet! The looping makes it a lot easier.

    Thanks a lot.

  • I'm sorry, but how would I do the calculations if I wanted to display 2 decimals?

  • Just replace the 10s with 100s.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)