1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
Option Explicit
Private Sub Command1_Click()
Text3 = Arrondir(Text1, Text2)
End Sub
'------------------------------------------------------------------------------------
'Arrondi d'un nombre quelconque, positif ou négatif avec valeur d'arrondi quelconque
'Entrée :
' ValIn : Nombre à arrondir
' Arrondi : Arrondi
'Sortie :
' Arrondir : Valeur arrondie
'------------------------------------------------------------------------------------
Public Function Arrondir(ValIn As Double, Arrondi As Double) As Double
Dim Entier As Long 'Partie entière du nombre à arrondir
Dim Reste As Double 'Différence entre partie entière et nombre à arrondir
Dim i As Integer 'Pour boucle
'Cas d'exclusion
If Arrondi > 1 Then
MsgBox "La valeur d'arrondi doit être inférieure ou égal à 1", vbExclamation, "Erreur"
Arrondir = 0
Exit Function
End If
If ValIn = 0 Then
Arrondir = 0
Exit Function
End If
Entier = Fix(ValIn)
Reste = Abs(ValIn - Entier)
Do
i = i + 1
If Reste - (i * Arrondi) <= Arrondi / 2 Then Exit Do
Loop
Arrondir = IIf(ValIn > 0, Entier + i * Arrondi, Entier - i * Arrondi)
End Function |
Partager