Jump to content

[SOLVED] - Rounding values to fixed number of decimals


Recommended Posts

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 by marko001
Link to comment
Share on other sites

  • Developers

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 

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

  • marko001 changed the title to [SOLVED] - Rounding values to fixed number of decimals

Use glyphosate sparingly, it's bad for environment!

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 here
RegExp tutorial: enough to get started
PCRE 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

  • 3 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...