Showing only 2 digits of a floating variable
BlitzMax Forums/BlitzMax Beginners Area/Showing only 2 digits of a floating variable
| ||
| Hello my friends :) My game goes very very nice. I try to cut the digits of the floating variable to only 2. I am counting how long time need my game to be loaded and I subtract the difference in milliseconds between the program starts , and when the game begins. Including loading time , splash screens and everything trying to optimize the loading time. I manage to convert whole time in millisecond but the floating value have a lots of digits after floating point. I would like to ask if you have any idea how to cut the digits to only 2 after the floating point. {code} 'At the starting of the program loading_count = MilliSecs() . 'Splash screens . 'Load data . 'Preparing everything 'At the beginning of the game loaded_time = MilliSecs() 'Calculate the difference DrawText "Game Loaded in: " + String((loaded_time - loading_count) / 1000) + "ms", 0, 10 {/code} The Game Loaded in 24.76564245642 ms Instead of 24.76 :) |
| ||
You could use a text Formatting module, or something more problem-specific like this :
SuperStrict
Framework brl.standardio
Print Rounded(24.76564245642)
Function Rounded:String(value:Float)
Local i:Int = value * 100
Return (i / 100) + "." + ("0" + i Mod 100)[1..]
End Function
Although if you want it to round up to the nearest hundredth you'll need some more logic. |
| ||
| This is how I (and others) round numbers: https://github.com/GWRon/Dig/blob/master/base.util.math.bmx Especially: NumberToString:String(value:Double, digitsAfterDecimalPoint:int = 2) It rounds your 24.765642 ms to "24.76" as it is mathematically correct. Bruceys function "cuts" of the value - which works most of the times, I had it happen that "0.00000xxx" gets handled as "-2xxxxx" (the minimum negative value). That is why I handle that differently. bye Ron |
| ||
| Id advice the text forming without any rounding, on the grounds that "if" five thousands of a second are important, display three digits However I would round with print (Int a*100) And unit it as Hundred thousandths of a second |
| ||
| Hi, just for sheer fun here is a another: SuperStrict Extern Function printf(format$z, d:Double) EndExtern Local d:Double = Pi Local format:String = "%.2f" printf (format, d) -Henri |
| ||
| Is that printf crossplatform? I am not sure but thought there were some "printf" like functions flying around - yours would of course add some kind of convenience. bye Ron |
| ||
| Printf is standard C so yes it should be. -Henri |
| ||
| BaH.Format uses snprintf under the hood. |
| ||
| Yes I believe Mark is using similar string2numeric & vice versa conversion in Blitzmax. -Henri |
| ||
| I assume there is no real "printf"-replacement possible because of "int" etc being no objects so you cannot have a "mixed array" param in the likes of printf(formatString, [intParam, stringParam, ...]) without converting intParam to strings etc ... a pitty but surely a restriction of the missing overloading-functionality in BlitzMax. Removes a lot of convenience. Nonetheless thanks for the hint. bye Ron |
| ||
| It seems the rounded function above sold my problem. Thank you very very much. |