Round a number to n decimal

BlitzPlus Forums/BlitzPlus Programming/Round a number to n decimal

goux(Posted 2015) [#1]
Perhaps it can help someone ;)

number#=10.5589345
decimal=2
Gosub round

Print arrondi#

Repeat
Until inkey$<>""
End

.round
arrondi#=Int(number#*10^decimal+1/2)/10^decimal
Return


ziggy(Posted 2015) [#2]
gosub? Nooooooooo!!!

It's several orders of magnitud better to create a function for this kind of uses


goux(Posted 2015) [#3]
Arfe, yep :(

Could use function too !


H&K(Posted 2015) [#4]
Maybe using gosub is ok, but not when the "sub-program/function" is only called once.
Geez inline it


Matty(Posted 2015) [#5]
I cannot remember the last time I used a gosub.....perhaps in an ancient time, a previous millenia, on an old machine that only had a command line interface in an archaic language known as GW-BASIC.....in the days when you had to number your own lines of code....


RemiD(Posted 2015) [#6]
That's a good approach, another approach would be to convert the float to a string, to create a new string, to read the chars before the . and the number of chars you want the . and add them to the new string, then to convert this new string to your new float.


_PJ_(Posted 2015) [#7]
Rounding OFF to n Decimal places:
Function RoundDec#(AFloat#,n=2)
	AFloat=Float(Int(AFloat*(10.0^n)))/(10.0^n)
		Return AFloat
End Function


Limiting to n Decimal places in length:
Function LimitDec#(AFloat#,n=2)
	Return Float(Left(Str(AFloat),Instr(Str(AFloat),".")+n))
End Function