Dim RoundedString As String = FormatNumber(Rounded, Decimals, , , TriState.False)
should do it for you
Pregunta
when I call this function like this Rounded(50.080, 3) Im getting "50.08" how can I force to keep the trailing zeros?
Public Function Rounded(ByVal Number, ByVal Decimals)
Rounded = Int(Number * 10 ^ Decimals + 1 / 2) / 10 ^ Decimals
End Function
Solución
Dim RoundedString As String = FormatNumber(Rounded, Decimals, , , TriState.False)
should do it for you
Otros consejos
Please, for your own health, turn Option Strict
and Option Explicit
on, stop using the return variable unless you really want to, and drop the legacy VB6 functions (like Int
).
Then you can probably hack something together with a string format like this:
Public Function Rounded(number As Decimal, decimals As Integer) As String
Return number.ToString("0." & New String("0"c, decimals))
End Function
It’s generally better to just use the format string yourself if you can, though, as in num.ToString("0.000")
for three decimal places, and drop Rounded
entirely.
You can use decimal.Round(Value, 2, MidpointRounding.AwayFromZero)
,
see more details here