Round a number to n decimal
BlitzPlus Forums/BlitzPlus Programming/Round a number to n decimal
| ||
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 |
| ||
gosub? Nooooooooo!!! It's several orders of magnitud better to create a function for this kind of uses |
| ||
Arfe, yep :( Could use function too ! |
| ||
Maybe using gosub is ok, but not when the "sub-program/function" is only called once. Geez inline it |
| ||
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.... |
| ||
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. |
| ||
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 |