Bonjour,
Contrairement à la fonction Round qui arrondi un nombre à sa valeur la plus proche en fonction des décimales choisies, je vous propose la fonction RoundUP qui arrondi un nombre à sa valeur supérieure en fonction des décimales choisies et, pour ne pas me faire couper l'herbe sous le pied, la fonction inverse RoundDown !
Voici le code et des exemples pour désherber tout ça :
Exemples :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 Public Function RoundUp(vValeur As Variant, Optional byNbDec As Byte) As Variant RoundUp = -Int(-vValeur * 10 ^ byNbDec) / 10 ^ byNbDec End Function Public Function RoundDown(vValeur As Variant, Optional byNbDec As Byte) As Variant RoundDown = Int(vValeur * 10 ^ byNbDec) / 10 ^ byNbDec End Function
Amicalement,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 MsgBox "Round: " & Round(5.2) ' 5 MsgBox "RoundUp: " & RoundUp(5.2) ' 6 MsgBox "RoundDown: " & RoundDown(5.2) ' 5 MsgBox "Round: " & Round(0.5) ' 0 BUG ???????????????? MsgBox "RoundUp: " & RoundUp(0.5) ' 1 MsgBox "RoundDown: " & RoundDown(0.5) ' 0 MsgBox "Round: " & Round(-0.5) ' 0 BUG ???????????????? MsgBox "RoundUp: " & RoundUp(-0.5) ' 0 MsgBox "RoundDown: " & RoundDown(-0.5) ' -1 MsgBox "Round: " & Round(2.51, 1) 'renvoie 2,5 MsgBox "RoundUp: " & RoundUp(2.51, 1) 'renvoie 2,6 MsgBox "RoundDown: " & RoundDown(2.51, 1) 'renvoie 2,5 MsgBox "Round: " & Round(-2.56, 1) 'renvoie -2,6 MsgBox "RoundUp: " & RoundUp(-2.56, 1) 'renvoie -2,5 MsgBox "RoundDown: " & RoundDown(-2.56, 1) 'renvoie -2,6
Philippe
Partager