Navigation

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

    printing a set amount of decimal places

    Help
    4
    16
    579
    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.
    • 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