Printing a float with 2 decimals

Monkey Forums/Monkey Programming/Printing a float with 2 decimals

Raul(Posted 2014) [#1]
Again, I cannot find this in documentation. how to print my float variable: profileTraceTime with 2 decimals? something like: 10.23


Trez(Posted 2014) [#2]
See if this helps

Function Round:Float(value:Float, decimalplaces:Int = 0)
	
	Local factor:Float = Pow(10, decimalplaces)
	value = value * factor
	Local rounded:Int = value + (Sgn(value) * 0.5)	
	Return Float(rounded / factor)

End


then call it like this

Print(Round(10.9823737))
Print(Round(10.9423737, 1))
Print(Round(10.9823737, 2))
Print(Round(10.9823737, 3))



Goodlookinguy(Posted 2014) [#3]
I've been using these functions from my lib to round numbers. Monkey by default doesn't come with these functions; although it really should.

'Summary: Round a number.
Function Round:Float( num:Float )
	Return Int(num + 0.5 * Sgn(num))
End

'Summary: Round a number to a place.
Function Round:Float( num:Float, places:Int )
	Local x:Int = Pow(10.0, places)
	Return Round(num * x) / x
End



Raul(Posted 2014) [#4]
Thank you guys.


Samah(Posted 2014) [#5]
Diddy has the Format function that gives functionality similar to printf. Note that this returns the value as a string.
Documentation is in the source.
https://code.google.com/p/diddy/source/browse/src/diddy/format.monkey

Print(Format("%.2f", 3.1415926))
' prints 3.14



Danilo(Posted 2014) [#6]
Trez / Goodlookinguy:
Function Main()
    Local f:Float = 12.123456789
    Print( f )
    Print( Round(f,7) )
    Print( Round(f,8) )
    Print( Round(f,9) )
    Print( Round(f,10) )
End

' some sample outputs for targets HTML5, GLFW, STDCPP:
'
' Monkey X - Print( f )
'
' 12.123456789
'
' Trez:
'
' 2.1474836470000001
' 0.2147483647
' -0.761445099
' 0.0975483602
' 
' Goodlookinguy:
'
' -0.761445099
' -0.060282409254025185
' 2.1474836470000001
' 1.0


Wanted to incorporate the Round() by Trez / Goodlookinguy into the Str() function below and found the above problems
when testing different targets.

So the following just cuts the string 'x' places after comma if too long, without any rounding:
Function Str:String(_float:Float, maxPlaces:Int=10)
    Local s:String = String(_float)
    Local i:Int = s.Find(".")
    If maxPlaces < 1 Then maxPlaces = 1
    If i = -1 Return s Else Return s[..i+1+maxPlaces]
End

Function Str:String(_bool:Bool)
    If _bool=True Return "True" Else Return "False"
End

Function Str:String(_integer:Int)
    Return String(_integer)
End


Function Main()

    Local f:Float, b:Bool, i:Int

    f = 12.1234567
    Print( Str(f)   )
    Print( Str(f,2) )
    Print( Str(f,12) )
    Print("----------")

    f = 12.123456789
    Print( Str(f)   )
    Print( Str(f,2) )
    Print( Str(f,12) )
    Print("----------")

    f = 3.1415926
    Print( Str(f)   )
    Print( Str(f,2) )
    Print("----------")

    f = 5.5678
    Print( Str(f)   )
    Print( Str(f,2) )
    Print("----------")

    f = 3.1
    Print( Str(f)   )
    Print( Str(f,2) )
    Print("----------")

    f = 5
    Print( Str(f)   )
    Print( Str(f,2) )
    Print("----------")

    f = 123456789
    Print( Str(f)   )
    Print( Str(f,2) )
    Print("----------")


    b = True
    Print( Str(b) )
    b = False
    Print( Str(b) )
    Print("----------")


    i = 123456789
    Print( Str(i) )
    Print("----------")
        
End

Still far from perfect and not exactly same output on all targets.
GLFW on Mac OS X for example seems to use lower precision floats than HTML5/STDCPP targets.


Goodlookinguy(Posted 2014) [#7]
...most people aren't using the round functions to round to more than 3 decimal places...

Anyways, JS uses a double for all numbers and the problem you're running into a precision limitation. My code works fine when used as expected. Outside of that, you're on your own.

Edit: In order to round off that string you're going to have to loop through it with right to left to deal with 9.

Edit 2: Heh, I just mocked this up. It seems to work.


Its fundamental limitation is that of non-scientific notation formed floats. So 3.4e+203 will not give you the expected output.