|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Rounding Negative Number Toward ZeroHello,
Is there a way in VB6 to round negative numbers towards zero when the deciding number is 5? For example, if my number is -0.245, and I would like to round to 2 decimal places, I would like the number rounded to -0.24. Currently, I'm using the formula below for asymmetric arithmetic rounding which rounds the previously mentioned number to -0.25. Formula: CDbl(FormatNumber(myNum, desiredDecimalPlaces)) Thanks in advance!! Jack > Is there a way in VB6 to round negative numbers towards zero when the Try something like this:> deciding number is 5? For example, if my number is -0.245, and I would like > to round to 2 decimal places, I would like the number rounded to -0.24. > Currently, I'm using the formula below for asymmetric arithmetic rounding > which rounds the previously mentioned number to -0.25. > > Formula: > CDbl(FormatNumber(myNum, desiredDecimalPlaces)) '*** Private Function RoundDP(ByVal inVal As Double, _ Optional ByVal inPlaces As Byte = 2) As Double Dim MultAmt As Long ' Note: Rounds towards 0 MultAmt = 10 ^ inPlaces RoundDP = Fix(inVal * MultAmt) / MultAmt End Function '*** Hope this helps, Mike - Microsoft Visual Basic MVP - E-Mail: ED***@mvps.org WWW: Http://EDais.mvps.org/ Mike,
Thanks for the response. That's close but it rounds everything towards zero. What my client requires is that just where 5 is the deciding number to round up or down, it should round down for negative numbers. So, here is what your code provides: -0.245 rounds to -0.24 = good. -0.246 rounds to -0.24 = my client wants -0.25. So, what I decided to do is create a condition in my procedure and force the value being passed in down to an appropriate number so it'll round the way I want. Here is my code: **************************************************** Private Function RoundValue(ivalue As Double, iDecimalPlaces As Integer) As Double If ivalue < 0 Then ivalue = ivalue + (0.1 ^ (iDecimalPlaces + 1)) RoundValue = CDbl(FormatNumber(ivalue, iDecimalPlaces)) Else RoundValue = CDbl(FormatNumber(ivalue, iDecimalPlaces)) End If End Function ************************************************ Jack Show quoteHide quote "Mike D Sutton" wrote: > > Is there a way in VB6 to round negative numbers towards zero when the > > deciding number is 5? For example, if my number is -0.245, and I would like > > to round to 2 decimal places, I would like the number rounded to -0.24. > > Currently, I'm using the formula below for asymmetric arithmetic rounding > > which rounds the previously mentioned number to -0.25. > > > > Formula: > > CDbl(FormatNumber(myNum, desiredDecimalPlaces)) > > Try something like this: > > '*** > Private Function RoundDP(ByVal inVal As Double, _ > Optional ByVal inPlaces As Byte = 2) As Double > Dim MultAmt As Long ' Note: Rounds towards 0 > > MultAmt = 10 ^ inPlaces > RoundDP = Fix(inVal * MultAmt) / MultAmt > End Function > '*** > > Hope this helps, > > Mike > > > - Microsoft Visual Basic MVP - > E-Mail: ED***@mvps.org > WWW: Http://EDais.mvps.org/ > > > "Jack" <J***@discussions.microsoft.com> wrote Another way to skin that cat:> > So, what I decided to do is create a condition in my procedure and force the > value being passed in down to an appropriate number so it'll round the way I > want. Here is my code: Function RoundOff(ByVal Value As Double) As Double RoundOff = Int(Value * 100 + 0.5) / 100 * Sgn(Value) End Function LFS |
|||||||||||||||||||||||