printing a set amount of decimal places
-
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?
-
Give this a try @hammy_
-
@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
-
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! -
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
-
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!
-
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
-
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
-
@Hitomi said in printing a set amount of decimal places:
I made a function like this
Yours is nicer. I'm deleting mine haha
-
@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.
-
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
-
@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"
-
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
-
@MikeDX why use int() in str(int(w)) and str(int(_2dp))? is that just to make sure there's no decimals after calculations?
-
@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.
-
after some mistakes on my part due to mixing languages, i managed to get it sorted, thanks a bunch!