General |
6.83 Arrotondare un numero alla cifra decimale specificata (uso commerciale) |
Karl Donaubauer |
Function fctRound(Optional varNr, Optional varPl As Integer = 2) ' by Konrad Marfurt + ? + Karl Donaubauer 'varNr: numero da arrotondare 'varPI: numero di decimali (due predefinito) If IsMissing(varNr) Or Not IsNumeric(varNr) Then Exit Function fctRound = Fix("" & varNr * (10 ^ varPl) + Sgn(varNr) * 0.5) / (10 ^ varPl) End FunctionEsempi: Se si omette varPI, il numero viene arrotondato alla seconda cifra decimale ?fctRound(1.024) --> 1.02 ?fctRound(1.025) --> 1.03 ?fctRound(1.24, 1) --> 1.2 ?fctRound(1.25, 1) --> 1.3 ?fctRound(1240, -2) --> 1200 ?fctRound(1250, -2) --> 1300 Note: - La stringa vuota ("") non è un errore, ma un trucco per essere più precisi nell'arrotondamento. - Se la funzione fornisce risultati falsi, verificate il tipo del campo numerico in tabella. Per esempio, il tipo Precisione semplice (Single) non è sufficente per fare un'arrotondamento a due cifre decimali, in quanto spesso non è preciso alla terza cifra decimale; nel qualcaso e' meglio utilizzare il tipo Precisione doppia (Double) o meglio ancora il tipo Valuta per moneta. |