Dopinder
Dopinder

Reputation: 11

How to have a custom rounding function?

Basically, I need FormatNumber to behave slightly differently. Of course, we can write our own MyFormatNumber, but basically, it should return the following:

59.080 returns 59.08
59.081 returns 59.08
59.082 returns 59.08
59.083 returns 59.08
59.084 returns 59.09
59.085 returns 59.09
59.086 returns 59.09
59.087 returns 59.09
59.088 returns 59.09
59.089 returns 59.09

As you can see, the only difference is when the 3rd decimal place is a 4, then I would like to round up. That is, when the input's third decimal place is 4 or greater, then do a round up, otherwise round down.

How can we do such a function in Classic ASP?

Upvotes: 0

Views: 156

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

A custom rounding function could be implemented like this:

Function CustomRound(n)
  If n * 1000 Mod 10 < 4 Then
    CustomRound = Int(n * 100) / 100
  Else
    CustomRound = (Int(n * 100) + 1) / 100
  End If
End Function

Upvotes: 1

Related Questions