marko001 Posted April 22, 2022 Share Posted April 22, 2022 (edited) Hi all guys/girls, I have an issue calculating a correct rounded value when selling/purchasing crypto values: This is the issue: Assets have a fixed max number of decimals that can be used. So if I have to buy, i.e. 7.00004374 FLUX where FLUX has 4 decimals max, I need to purchase the higher rounded value with max 4 decimals. #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form=Y:\__ AutoIt\BITBOT\GUI_BitBot\tet2.kxf $Form1 = GUICreate("frm1", 199, 151, 485, 272) $Input1 = GUICtrlCreateInput("7.00004374", 64, 8, 121, 21) $Label1 = GUICtrlCreateLabel("Value", 8, 10, 34, 17) $Label2 = GUICtrlCreateLabel("Decimals", 8, 46, 44, 17) $Input2 = GUICtrlCreateInput("4", 64, 44, 121, 21) $Label3 = GUICtrlCreateLabel("Result", 8, 82, 45, 17) $Input3 = GUICtrlCreateInput("", 64, 80, 121, 21) $Button1 = GUICtrlCreateButton("Calc", 8, 112, 179, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 GUICtrlSetData($Input3,_DoCalc(GUICtrlRead($Input1),GUICtrlRead($Input2))) EndSwitch WEnd Func _DoCalc($value,$decimals) $answer = Round($value,$decimals) Return $answer EndFunc Using the classic Round() doesn't solve it Correct result should be (some examples here below): 7.00004374 -> (max 2 decimals) -> 7.01 7.00004374 -> (max 3 decimals) -> 7.001 7.00004374 -> (max 4 decimals) -> 7.0001 7.00004374 -> (max 5 decimals) -> 7.00005 7.00004374 -> (max 6 decimals) -> 7.000044 7.00004374 -> (max 7 decimals) -> 7.0000438 How can I manage it? Thanks in advance, Marco Edited April 22, 2022 by marko001 Link to comment Share on other sites More sharing options...
Developers Jos Posted April 22, 2022 Developers Share Posted April 22, 2022 You need an RoundUp function for that. one way of doing that is: $input = 7.00004374 ConsoleWrite(_RoundUp( $input, 0) & @CRLF) ConsoleWrite(_RoundUp( $input, 1) & @CRLF) ConsoleWrite(_RoundUp( $input, 2) & @CRLF) ConsoleWrite(_RoundUp( $input, 3) & @CRLF) ConsoleWrite(_RoundUp( $input, 4) & @CRLF) ConsoleWrite(_RoundUp( $input, 5) & @CRLF) ConsoleWrite(_RoundUp( $input, 6) & @CRLF) ConsoleWrite(_RoundUp( $input, 7) & @CRLF) ConsoleWrite(_RoundUp( $input, 8) & @CRLF) ConsoleWrite(_RoundUp( $input, 9) & @CRLF) ConsoleWrite(_RoundUp( $input, 10) & @CRLF) Func _RoundUp( $iValue, $iDec) $oValue = Round($iValue,$iDec) If $oValue < $iValue Then $oValue = Round($iValue + (0.5 / (10 ^ $idec)),$iDec) EndIf return $oValue EndFunc result: 8 7.1 7.01 7.001 7.0001 7.00005 7.000044 7.0000438 7.00004374 7.00004374 7.00004374 Jos Skysnake and marko001 1 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
marko001 Posted April 22, 2022 Author Share Posted April 22, 2022 Wow, perfect! works exaclty as intended! Thanks a lot! Link to comment Share on other sites More sharing options...
jchd Posted April 22, 2022 Share Posted April 22, 2022 Use glyphosate sparingly, it's bad for environment! Jos and marko001 2 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Skysnake Posted May 7, 2022 Share Posted May 7, 2022 The problem was that currency != numbers... Skysnake Why is the snake in the sky? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now