|
Post by harris on Jan 20, 2019 20:54:54 GMT -5
I want to write a program that calculates storm water volume in a pond based on a spread sheet that I use. The calculation gives numbers with several digits after the decimal place such as 2.046691097. I want to round these answers to the hundreds place but I cant find a rounding function in Liberty Basic. What am I missing?
|
|
|
Post by metro on Jan 21, 2019 0:36:44 GMT -5
|
|
|
Post by mknarr on Jan 21, 2019 11:47:19 GMT -5
Here is another way to round a number
ans=2 for x=0 to 10 ans=2+x/10 print ans, gosub [RoundNumber] print roundans next end
[RoundNumber] roundans=Int(ans+.5) return
|
|
|
Post by harris on Jan 21, 2019 13:42:07 GMT -5
Thank you for the responses. I will try both approaches.
|
|
|
Post by Brandon Parker on Jan 21, 2019 23:48:47 GMT -5
I find that these two functions are very useful. The ConvertToDouble$() function can be used to ensure that a value is coerced to being an actual floating point value when storing a number into a Struct member that is set up as a Double with respect to its type; attempting to store anything else ends in a protection violation. The added benefit is that the function provides rounding as well. The integerPart was included for possible future Limit functionality, but it serves no purpose at the moment.
num = 665.53256 'The function will not Limit the integer portion in its current state. 'Use Val to change it to a numerical value instead of a string. Print Val(ConvertToDouble$(num, 2, 3)) Wait
'____________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________
Function ConvertToDouble$(value, integerPart, decimalPart) 'integerPart does not become limited currently; included for possible future limit option integerPart$ = string$("#", integerPart) decimalPart$ = string$("#", decimalPart) ConvertToDouble$ = Trim$(RemChar$(Using(integerPart$;".";decimalPart$, value), "%")) End Function
'____________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________
Function string$(myString$, numstring) For i = 1 To numstring string$ = string$ + myString$ Next i End Function
{:0)
Brandon R. Parker
|
|