Showing only 2 digits of a floating variable

BlitzMax Forums/BlitzMax Beginners Area/Showing only 2 digits of a floating variable

Takis76(Posted 2015) [#1]
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 :)


Brucey(Posted 2015) [#2]
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.


Derron(Posted 2015) [#3]
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


H&K(Posted 2015) [#4]
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


Henri(Posted 2015) [#5]
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


Derron(Posted 2015) [#6]
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


Henri(Posted 2015) [#7]
Printf is standard C so yes it should be.

-Henri


Brucey(Posted 2015) [#8]
BaH.Format uses snprintf under the hood.


Henri(Posted 2015) [#9]
Yes I believe Mark is using similar string2numeric & vice versa conversion in Blitzmax.

-Henri


Derron(Posted 2015) [#10]
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


Takis76(Posted 2015) [#11]
It seems the rounded function above sold my problem. Thank you very very much.