Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    printing a set amount of decimal places

    Help
    4
    16
    581
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hammy_ last edited by

      Basically i have a timer that uses a decimal place to seperate seconds and frames, everytime 60 frames passes the seconds counter goes up by one, simple stuff, but it's printing 6 decimal places rather than the 2 i need, is there any way to stop the extra 4 decimals from being printed?

      1 Reply Last reply Reply Quote 0
      • M
        MikeDX last edited by

        Give this a try @hammy_

        https://fuzearena.com/help/view/trunc

        H 1 Reply Last reply Reply Quote 0
        • H
          hammy_ @MikeDX last edited by

          @MikeDX this would help, the only problem is i still want 2 of the decimal places to be displayed and not the extra ones that just end up being 0, my best idea right now is to just throw a black box on top of them so they can't be seen

          1 Reply Last reply Reply Quote 0
          • M
            MikeDX last edited by

            Sorry, I should have paid attention. That' not what you want at all.

            You could convert to string using str(n) and then chop the ends off. I'm not even sure if thats a sane workaround!

            1 Reply Last reply Reply Quote 0
            • H
              hammy_ last edited by

              that's definitely a shout, not sure if it'd be worth the hassle though, just from thinking for far too long wouldn't i need to convert back to a float once it's printed? i also can't find anything to shorten the length of a string but that's probably just me being me, as usual

              1 Reply Last reply Reply Quote 0
              • M
                MikeDX last edited by

                I suppose for printing, it doesn't matter. Did you only want to do calculations on the original float? You can convert it back anyway with float(n) once you've printed if you wanted to do that.

                Seems a lot of of hassle to print to 2dp.. I'll add it to the list of nice to haves!

                1 Reply Last reply Reply Quote 0
                • H
                  hammy_ last edited by

                  the only calculations i need to do is just adding a number each frame, though wouldn't that be impossible if it's a string? In that case i'd need to convert it back so the entire thing works as intended. It's not a massive issue, just a qualty of life thing, i'll just put a box in front of the unwanted digits considering i'm not doing anything with the background

                  1 Reply Last reply Reply Quote 0
                  • Hitomi
                    Hitomi F last edited by MikeDX

                    I made a function like this

                    //returns string with floating portion rounded to dec number of digits
                    function float2str(arg, dec)
                      dec = pow(10, dec)
                      result = str(int(arg)) + "." + int(round(fract(arg) * dec))
                    return result
                    
                    M N 2 Replies Last reply Reply Quote 3
                    • M
                      MikeDX @Hitomi last edited by

                      @Hitomi said in printing a set amount of decimal places:

                      I made a function like this

                      Yours is nicer. I'm deleting mine haha

                      1 Reply Last reply Reply Quote 1
                      • N
                        Nisse5 F @Hitomi last edited by Nisse5

                        @Hitomi Yep, that's how I do it as well. The only difference is that I start number-starting strings with ""+int(i) rather than str(int(i)), but that's just cosmetics.

                        1 Reply Last reply Reply Quote 0
                        • H
                          hammy_ last edited by

                          damn, all of this is going straight over my head, even understanding a few of the terms used doesn't help at all, never realised something so seemingly simple could end up so complicated

                          1 Reply Last reply Reply Quote 0
                          • M
                            MikeDX last edited by MikeDX

                            @hammy_ it's easier than it looks.

                            You want two pieces of information. The whole number a decimal place and finally the 2dp of your float

                            to get the 2dp, you can do this

                            // n = 1.988383
                            w = trunc(n) // 1
                            f = fract( n )  // 0.988383
                            _2dp = trunc(n*100)  // 98 
                            string = str(int(w)) + "." + str(int(_2dp)) // 1 + . + 98  = "1.98" 
                            
                            H 1 Reply Last reply Reply Quote 0
                            • Hitomi
                              Hitomi F last edited by

                              I put in round also, without that it can sometimes be too low. 99.6 prints out as 99.59998 so to avoid it printing as 99.59 I have it round after I multiplied it by 100

                              1 Reply Last reply Reply Quote 1
                              • H
                                hammy_ @MikeDX last edited by

                                @MikeDX why use int() in str(int(w)) and str(int(_2dp))? is that just to make sure there's no decimals after calculations?

                                M 1 Reply Last reply Reply Quote 0
                                • M
                                  MikeDX @hammy_ last edited by

                                  @hammy_ said in printing a set amount of decimal places:

                                  why use int() in str(int(w)) and str(int(_2dp))? is that just to make sure there's no decimals after calculations?

                                  That's right. as trunc and fract both return floats.

                                  1 Reply Last reply Reply Quote 0
                                  • H
                                    hammy_ last edited by

                                    after some mistakes on my part due to mixing languages, i managed to get it sorted, thanks a bunch!

                                    1 Reply Last reply Reply Quote 1
                                    • First post
                                      Last post