' ################################################################################################################################################################
' Rounding test
' ################################################################################################################################################################
' BOOLEAN CONSTANTS
' GLOBAL VARIABLES a$=string, i%=integer, L&=long, s!=single, d#=double
' START THE MAIN PROGRAM
main ProgramName$
' FINISH UP
SYSTEM ' return control to the operating system PRINT ProgramName$
+ " finished."
' /////////////////////////////////////////////////////////////////////////////
' Rounding and math.
' http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index_PHPSESSID_gulg2aoa966472fnfhjkgp4i35_topic_14266-0/
'
' Rounding up to n decimal places?
' https://www.qb64.org/forum/index.php?topic=3605.0
' Quote from: SMcNeill on May 16, 2017, 06:57:17 pm
' Can also try:
' INT(number * 100)/100
' Now that worked.
' STR$(INT(myprice * 100) / 100)
' Perfectly drops all the numbers to 2 decimal places.
' What a relief. Thank you so much and everyone else who gave advice. :)
' Quote from: bplus on Today at 02:13:29 PM
' There is round Keyword check Wiki, might be _round
' you have to add 1/2 of 10 ^ DP to x
' EDIT: crap it's .5 * (1/10^DP)
DIM dp%
' # decimal places
iTotal = 0
PRINT "Rounds single and double precision numbers." PRINT "Thanks to SMcNeill, bplus, and Pete for your help."
' ROUND SINGLE PRECISION NUMBERS TO 3 DECIMAL PLACES
iColumn = 1
iLine1 = 1: arrOutput(iLine1, iColumn) = "SINGLE TO 3 DECIMAL PLACES"
iLine1 = 2: arrOutput(iLine1, iColumn) = RightPadString$("Original", 10, " ") + " " + RightPadString$("Rounded", 10, " ")
dp% = 3
iLine1 = iLine1 + 1
s2! = s1! + (.5 * (1 / (10 ^ dp%)))
s2!
= INT(s2!
* (10 ^ dp%
)) / (10 ^ dp%
) arrOutput(iLine1, iColumn) = RightPadString$(SngToStr$(s1!), 10, " ") + " -> " + RightPadString$(SngToStr$(s2!), 10, " ")
iMaxLines = iLine1
' ROUND DOUBLE PRECISION NUMBERS TO 3 DECIMAL PLACES
iColumn = 2
iLine2 = 1: arrOutput(iLine2, iColumn) = "DOUBLE TO 3 DECIMAL PLACES"
iLine2 = 2: arrOutput(iLine2, iColumn) = RightPadString$("Original", 10, " ") + " " + RightPadString$("Rounded", 10, " ")
dp% = 3
iLine2 = iLine2 + 1
d2# = d1# + (.5 * (1 / (10 ^ dp%)))
d2#
= INT(d2#
* (10 ^ dp%
)) / (10 ^ dp%
) arrOutput(iLine2, iColumn) = RightPadString$(DblToStr$(d1#), 10, " ") + " -> " + RightPadString$(DblToStr$(d2#), 10, " ")
iMaxLines = iLine2
arrOutput(iLine2, 1) = ""
FOR iLine1
= 1 TO iMaxLines
PRINT RightPadString$
(arrOutput
(iLine1
, 1), 30, " ") + " " + RightPadString$
(arrOutput
(iLine1
, 2), 30, " ")
INPUT "PRESS <ENTER> TO CONTINUE", in$
' /////////////////////////////////////////////////////////////////////////////
' Scientific notation - QB64 Wiki
' https://www.qb64.org/wiki/Scientific_notation
' Example: A string function that displays extremely small or large exponential decimal values.
Xpos%
= INSTR(value$
, "D") + INSTR(value$
, "E") 'only D or E can be present expo%
= VAL(MID$(value$
, Xpos%
+ 1)) sign$
= "-": valu$
= MID$(value$
, 2, Xpos%
- 2) dot%
= INSTR(valu$
, "."): L%
= LEN(valu$
) DblToStr$
= _TRIM$(sign$
+ DP$
+ min$
+ num$
+ add$
)
' /////////////////////////////////////////////////////////////////////////////
' Scientific notation - QB64 Wiki
' https://www.qb64.org/wiki/Scientific_notation
' Example: A string function that displays extremely small or large exponential decimal values.
Xpos%
= INSTR(value$
, "D") + INSTR(value$
, "E") 'only D or E can be present expo%
= VAL(MID$(value$
, Xpos%
+ 1)) sign$
= "-": valu$
= MID$(value$
, 2, Xpos%
- 2) dot%
= INSTR(valu$
, "."): L%
= LEN(valu$
) SngToStr$
= _TRIM$(sign$
+ DP$
+ min$
+ num$
+ add$
)
' /////////////////////////////////////////////////////////////////////////////
' By sMcNeill from https://www.qb64.org/forum/index.php?topic=896.0
IsNum% = TRUE
IsNum% = FALSE
' /////////////////////////////////////////////////////////////////////////////
FUNCTION RightPadString$
(myString$
, toWidth%
, padChar$
) RightPadString$
= LEFT$(myString$
+ STRING$(toWidth%
, padChar$
), toWidth%
)
' /////////////////////////////////////////////////////////////////////////////
'cstr$ = LTRIM$(RTRIM$(STR$(myValue)))