lunedì 31 ottobre 2011

[VB6] Arrotondamenti Diversificati con Select Case

Descrizione :
Un esempio su come usare una Select Case per eseguire un arrotondamento personalizzato di numeri in base a range prestabiliti.

+ Articolo :

Si sfrutta la possiblità di usare Case x To y : il che comporta l'esclusione dell'estremo sinistro (x) del range, e l'inclusione del destro (y).
In sostanza Case x To y equivale a : Valore > x AND <= y.

Nell'esempio di codice seguente si desidera arrotondare il numero in input secondo questi range :

Da 0 a 10 deve essere arrotondato al 0,10
Da 10,01 a 20 deve essere arrotondato al 1,00
Da 20,01 a 30 deve essere arrotondato al 2,00
Da 30,01 a 100 deve essere arrotondato al 3,00
Da 100,01 a 1000 deve essere arrotondato al 5,00
Da 1000,01 a 2000 deve essere arrotondato al 10,00
Da 2000,00 a 3000 deve essere arrotondato al 20,00
Da 3000,01 a 5000 deve essere arrotondato al 30,00
...

--> Codice Esempio :

    Dim valore As Double
    valore = 33.0005
 
    Dim valoreRound As Double
 
    Select Case valore
 
        Case 0 To 10
            valoreRound = Round(valore, 1)
            If valoreRound < valore Then valoreRound = valoreRound + 0.1
 
        Case 10 To 20
            valoreRound = Round(valore, 0)
            If valoreRound < valore Then valoreRound = valoreRound + 1
 
        Case 20 To 30
            valoreRound = Int(valore / 2) * 2 + 2
 
        Case 30 To 100
            valoreRound = Int(valore / 3) * 3 + 3
 
        '...
        '...
        '...
 
    End Select
 
    MsgBox valoreRound
Questi i primi 4 casi. Per i successivi non fate altro che ripetere il Caso 4 sostituendo con i valori desiderati...

+ Fine Articolo.

Un Click su "Mi Piace" è il modo migliore per ringraziare l'autore di questo articolo.



2 commenti:

Greg ha detto...

hi how are you? can you please give me a installer of vb6? thank you very2x much ...

MarcoGG ha detto...

Hi greg. As you certainly know, VB6 is NOT freeware, so I can't fulfill your request.
By the way, many of the VB6 snippets ( like this one ) can be tested in a VBA environment, just like the Excel VBA Editor.

Greetings And Happy Coding !

MarcoGG.

Posta un commento

Favorites Twitter Facebook Delicious Digg Stumbleupon More

 
Design by Free WordPress Themes Modificato da MarcoGG