Advertisement
Caius-Corsades

AutoIt UDF to perform actions on decimals numbers

Oct 27th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.16 KB | None | 0 0
  1. ; ==> _RoundDecimal($NbDouble, $NbForRound)
  2. ; This function takes two parameters:
  3. ; $NbDouble to take the number to be rounded. Datatype is double.
  4. ; $NbForRound to contain the precision of the rounding. Datatype is int
  5. ; This function return a variable of datatype double or int if rounding is zero or an @error code.
  6. ; @error:
  7. ;   1 => $NbDouble isn't number
  8. ;   2 => $NbForRound isn't integer
  9. ;   3 => $NbForRound ins't positive number.
  10. Func _RoundDecimal($NbDouble, $NbForRound)
  11.     ;   If $NbDouble isn't double datatype.
  12.     If IsFloat($NbDouble) <> 1 Then
  13.         If IsNumber($NbDouble) = 1 Then
  14.             Return $NbDouble
  15.         Else
  16.             SetError(1)
  17.             Return
  18.         EndIf
  19.     EndIf
  20.  
  21.     ;   If $NbToRound isn't integer datatype.
  22.     If IsInt($NbForRound) <> 1 Then
  23.         SetError(2)
  24.         Return
  25.     EndIf
  26.  
  27.     ; If $NbToRound isn't a  positive number
  28.     If $NbForRound <= 0 Then
  29.         SetError(3)
  30.         Return
  31.     EndIf
  32.  
  33.     ;   We transform $NbDouble in string to separate iteger part to decimal part
  34.     Local $StringNbDouble = String($NbDouble)
  35.     $StringNbDouble = StringSplit($StringNbDouble,".")
  36.  
  37.     Local $LenghtOfDoublePart = StringLen($StringNbDouble[2])
  38.  
  39.     ;   If the number of char to keep is greater than the real lenght of string.
  40.     If $NbForRound >= $LenghtOfDoublePart Then
  41.         Return $NbDouble
  42.     EndIf
  43.  
  44.     $StringNbDouble[2] = StringLeft($StringNbDouble[2], $NbForRound)
  45.     Local $Result = Number($StringNbDouble[1] & "." & $StringNbDouble[2])
  46.     Return $Result
  47. Return
  48. EndFunc
  49. ; <== _RoundDecimal($NbDouble, $NbForRound)
  50.  
  51.  
  52.  
  53. ; ==> _NbAfterDecimalPoint($NbDouble)
  54. ; This function takes one parameter:
  55. ; $NbDouble to take the number to be rounded. Datatype is double.
  56. ; This function return a variable of datatype int and contain the number of number after decimal point.
  57. ; @error:
  58. ;   1 => $NbDouble isn't number
  59. Func _NbAfterDecimalPoint($NbDouble)
  60.     If IsFloat($NbDouble) <> 1 Then
  61.         If IsNumber($NbDouble) = 1 Then
  62.             Return $NbDouble
  63.         Else
  64.             SetError(1)
  65.             Return
  66.         EndIf
  67.     EndIf
  68.  
  69.     Local $StringNbDouble = String($NbDouble)
  70.     $StringNbDouble = StringSplit($StringNbDouble,".")
  71.     Local $LenghtOfDoublePart = StringLen($StringNbDouble[2])
  72.     Return Int($LenghtOfDoublePart)
  73. EndFunc
  74. ; <== _NbAfterDecimalPoint($NbDouble)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement