Create a variable with fixed decimal places

0 favourites
From the Asset Store
Awardkit
$5 USD
Create custom awards and achievement or use the many templates and create your own.
  • So i have the event right to return me the value of billions with 3 decimal places but i am using this event on a fast timer and anytime it goes to a 0 it will round up to shorter decimal places, example being 123.130 becomes 123.13, I want to always keep 3 decimal places, looked on a lot of posts but can't seem to find the right answer, please help been stuck on this for a day now :(

    Set Text:round(Variable / 10^9 * 1000) / 1000

  • deleted bad answer

  • Thanks! :) this is a bit beyond me and it's only my 2nd week using construct 3, how would you write that?

    i current have every tick to check for Variable => 10^9 < 10^12 then i will go to => 10^12 < 10^15 for trillions etc

  • BillionCheck to int(Variable / 10^9 * 1000) /1000

    BillionCheck2 to tokenat("Beforedec . Afterdec",1,".")

    On every tick but just returning a Nan value.

    To be honest this just looks wrong as i post it but have no clue how to fix it

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Actually I am being stupid.

    Zeropad pads to the left. You need zeros to the right.

    So I am now thinking just count the chars after the decimal then add zeros as nessesary.

    hmm let me see...

  • BillionCheck to int(Variable / 10^9 * 1000) /1000

    Beforedec to tokenat(BillionCheck & "Beforedec . Afterdec",0,".")

    Afterdec to tokenat(BillionCheck & "Beforedec . Afterdec",1,".")

    Checkdec to len(str(Afterdec))

    every tick

    Checkdec = 2

  • a bit ineficiant but so you can see how it works

    make a text input field

    and a text (output)box

    then

    + System: len(tokenat(TextInput.Text, 1, ".")) = 0

    (This counts the characters after the decimal and checks if = 0)

    -> Text: Set text to tokenat(TextInput.Text, 0, ".")&"."&tokenat(TextInput.Text, 1, ".")&"000"

    (If true then this sets the text to the characters before the decimal + a decimal point + 000)

    Then just repeat for 1 2 3 i.e more characters after decimal less 0s

    + System: len(tokenat(TextInput.Text, 1, ".")) = 1

    -> Text: Set text to tokenat(TextInput.Text, 0, ".")&"."&tokenat(TextInput.Text, 1, ".")&"00"

    + System: len(tokenat(TextInput.Text, 1, ".")) = 2

    -> Text: Set text to tokenat(TextInput.Text, 0, ".")&"."&tokenat(TextInput.Text, 1, ".")&"0"

    + System: len(tokenat(TextInput.Text, 1, ".")) = 3

    -> Text: Set text to tokenat(TextInput.Text, 0, ".")&"."&tokenat(TextInput.Text, 1, ".")

  • Tried everything and it still flickers a little, it's a pity i can't just stop automatic rounding at all, even when it hits 2 characters and i just put & 0 it still flickers a little, so i just don't understand how to get it to work

  • What you mean?

    here no flicker.

    drive.google.com/open

  • I checked that project and they are not in timers, when adding just "0" it seemed fine but when adding "00" or "000" with "Billion" it would always flicker especially on a quick timer or every tick, so in the end i just added a separate text box that will display "Billion" or "Trillion" and so on and i get no flicker, i didn't use the str function though for the last part with "billion" so maybe it might of been the problem.

  • But i have a working solution with your help :) and learning how to separate the variable was key so i really thank you for it

  • Ok man , you should share a project then more people will pitch in. I still dont understand this flickering you are talking about. If you delete the "on space bar pressed" condition in my project it will generate every frame. 60 times a second and no flickering. I never used tokenat before either so good for me to learn!

    Anyway England are through so all is well.....

  • My project is pretty big because i am making a game but hopefully you can see the screenshot, the problem was with adding & "Billion" at the end to them 3 events, it would flicker sometimes on check 2 or check 1 and i just couldn't figure it out.

    Very happy England are through man!

    And i checked your project and removed the space bar but even on 60 frames a second you will see it flickers sometimes, it's something to do with the rounding i think but you can watch it and see it might jump sometimes which would make the word Billion jump and it looks strange.

  • OK cool. looks like you know what you are doing. thats a lot of hacks!

    If the flickering is just about the text spacing moving then you can use sprite fonts and set the spacing as you want. eg to all monospace

    in fact you should avoid using form text fields text boxes etc in any part of a game that needs performance. Updating html form stuff is very CPU incentive supposedly. instead use sprite fonts in game.

    the form stuff is more for editors and the like.

    (or possibly I think C3 has introduced web fonts but I dont know much about them havent played with them yet.)

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