Comment On Well ... that's one way to Roundoff ...

When it comes to talking about Microsoft Access, it seems that two tiny versions of me *poof* into existence, each sitting on a shoulder. The guy on my right wears a suit and always reminds me how great of a tool Access is because it empowers small organizations to develop productivity and information systems. The other guy, sporting a “l337 h4x0r“ tee and cut-off jeans, screams in my ear that “Access is a complete abomination“ and that the tools to create applications should not be put in the hands of laymen. [expand full text]
« PrevPage 1Next »

re: Well ... that's one way to Roundoff ...

2004-09-14 12:43 • by Johnie
Biggest abomination of VB is the lack of strict types and operations.

Take this one: Left(frac, 2) + 1
That's a [string] + [int] --> [string]
And because of the + sign, it converts the first string to an int.

Anyways, this one isn't as bad as some of the other WTFs

re: Well ... that's one way to Roundoff ...

2004-09-14 12:50 • by TJ
Im proud to say I dont know WTF this code is doing because I never had to touch access. I dont even want to know what the Fix Method does.

re: Well ... that's one way to Roundoff ...

2004-09-14 13:01 • by Bu
Ummm... I'm not VB-literate so I may be off here, but isn't this guy just trying to do something like...

Function roundoff(x)
roundoff = Int((x * 100) + .5) / 100
End Function

This is clearly a case of someone who should never have been allowed near a computer. Heck, near anything electric for that matter!

re: Well ... that's one way to Roundoff ...

2004-09-14 13:11 • by TheF0o1
What the frac?

Yet another example of how numeric operations are much harder to do with strings. Heh, that "-0" reminds me of the bogus bug reports that used to circulate around my workplace on April 1.

re: Well ... that's one way to Roundoff ...

2004-09-14 13:17 • by Alex Papadimoulis
Bu has successfully replaced the entire function code with a single line. Good job!

re: Well ... that's one way to Roundoff ...

2004-09-14 13:20 • by TheF0o1
LOL! I just looked up the Fix function in MSDN library. It "Returns the integer portion of a number". But the funniest part is that at the bottom of the page it says, "See also... Round Function". How many of us here know how to RTFM?

re: Well ... that's one way to Roundoff ...

2004-09-14 13:33 • by df
I haven't looked at this code enough to say anything about it specifically, but I can tell you from experience, don't throw stones at something like this just because there was already a function to do it. I've had to write special rounding functions before to match legacy systems that had rounding errors. (I am not making this up). Microsoft has a pretty standard rounding method now, but in the old days, different MS languages and apps had subtle differences in their rounding function.

re: Well ... that's one way to Roundoff ...

2004-09-14 13:33 • by Bu
ROFL TheF0o1 that's a good one!

re: Well ... that's one way to Roundoff ...

2004-09-14 13:33 • by Cakkie
OMG, someone shoot this person. Why do something the easy way when there's a hard way :p

A very very very interesting piece of code are these two lines, when dealing with negative numbers.

LenFrac = Right(frac, Len(frac) - 1)
If Len(LenFrac) = 2 Then

He first assigns all but 1 character to the integer LenFrac, basically stripping the - sign. Then he does a Len() on the integer. The thing is, Len accepts a variant, not a string. Therefore, the length of the integer is returned rather than the length of the string the integer represents. This means that no matter what value LenFrac has, Len() will always return 2 (since an integer is 2 bytes). This means that he will insert another -0 in front of the number, giving him a value that is 1/10 of what it should be :p

re: Well ... that's one way to Roundoff ...

2004-09-14 14:08 • by Bu
Tell the truth, df, you're the author of this abomination aren't you? LOL!

re: Well ... that's one way to Roundoff ...

2004-09-14 14:20 • by Manni
I only did a little testing on this code because otherwise the little man inside my computer would have come out and shot me...

I compared this to the Round() function built-in to VB and it returned the EXACT same value as this convoluted atrocity of a Function. To hell with replacing it with a single line, how about with a feature found in VB by default:

Round(x, 2)

What are the situations where this WON'T work?

re: Well ... that's one way to Roundoff ...

2004-09-14 14:53 • by Bu
Hey, I did say I was VB-challenged, but apparently not NEARLY as challenged as the author of this hideousness.

"The horror... The horror..."

re: Well ... that's one way to Roundoff ...

2004-09-14 15:06 • by Dz
Maybe this guy was getting ready to convert to .NET :
http://weblogs.asp.net/sfurman/archive/2003/03/07/3537.aspx

re: Well ... that's one way to Roundoff ...

2004-09-14 15:07 • by Dz
</sarcasm>

re: Well ... that's one way to Roundoff ...

2004-09-14 15:12 • by Bill Gates
My software was not intended to be coded in this fashion... Please take a computer class.
-Bill

re: Well ... that's one way to Roundoff ...

2004-09-14 15:39 • by Centaur
There was once a project I worked on that at one time had a function that basically returned a string representation of 1 + x, given a string representation of x, in a convoluted (and incorrect) way. Mind you, the code was in Delphi which has an integer type and everything necessary to convert between strings and integers.

re: Well ... that's one way to Roundoff ...

2004-09-14 16:38 • by Hassan Voyeau
"What are the situations where this WON'T work? "

Manni : try -0.1 it returns -0.01 as pointed out Cakkie.

re: Well ... that's one way to Roundoff ...

2004-09-14 16:40 • by Eric W. Bachtal
Bu - You're one-liner needs a CStr() call in order to match the original's expected string output. ;)

Manni - The VBA Round() method does banker's rounding, which the misguided author of roundoff probably didn't want. So, given 1.135, both routines will return 1.14 (assuming a second parameter of 2 to Round()), but given 1.145, Round() will return 1.14 while roundoff() (or Bu's one-liner) will return 1.15.

re: Well ... that's one way to Roundoff ...

2004-09-15 08:54 • by Nick D
What you want is the FormatNumber(number, X) function. This rounds a number to X decimal places AND returns it as a string into the bonus!

re: Well ... that's one way to Roundoff ...

2004-09-15 09:21 • by Jeff S
(I'm the lucky guy who found this)

The whole function is just wonderful, but my favorite part is this:

Result = Fix(x) + frac

Look at that carefully ... Result is a string, Fix() returns a numeric value, and frac is a string ... what does this do? Concatenation? Addition? Who knows !?

Just a work of art!

re: Well ... that's one way to Roundoff ...

2004-09-15 10:29 • by Manni
Hassan: I think you misunderstood me. Round() will return -0.1 if you pass -0.1 to it. I hope you mean that this guy's function screwed it up and returned -0.01.

Eric B.: I knew I remembered something funky about Round(). Thanks for letting me know. I swear I don't see the point of rounding to the nearest even number, there's gotta be some logic behind it though.

re: Well ... that's one way to Roundoff ...

2004-09-15 11:02 • by Steve O.
Rounding the nearest even number helps make it a wash to round. If you always rounded up, everything would be off to the high side. By rounding to the nearest number, it keeps rounding from skewing the numbers.

re: Well ... that's one way to Roundoff ...

2004-09-15 11:56 • by Cakkie
@Jeff S: just for the record, if there are any numeric datatypes involved, VB will always perform an Addition. Only when both are strings, a concatination is done. Since Fix always returns a number (unless Null is passed, in which case Null is returned), an addition will be used in this case.

"123" + "4" => "1234" -> two strings
"123" + 4 => 127 -> 1 string, 1 number
Fix("123.456") + "4" => 127 -> Fix returns a number

re: Well ... that's one way to Roundoff ...

2004-09-15 15:26 • by XTOCb
If Fix(x) is an integer value, and frac is a string representing a value between 0 and 1, then it doesn't matter whether the plus sign does concatenation or addition :)

re: Well ... that's one way to Roundoff ...

2004-09-16 04:14 • by Thomas Eyde
I have seen situations where rounding to the even number did not achieve what's meant to be. Coincidentily, it also happened in Access.

I had to share a certain number among 8 stakeholders, but when I added the numbers up again, I was always missing the .02.

You need a large number to justify Banker's rounding.

re: Well ... that's one way to Roundoff ...

2004-09-16 04:21 • by Thomas Eyde
In all fairness to VB, it has a decent type system, but you have to declare your types to get the advantage:

dim theNumber as double

re: Well ... that's one way to Roundoff ...

2004-10-26 09:29 • by gary longstaff
I agree

Re: Well ... that's one way to Roundoff ...

2005-01-27 12:17 • by
28555 in reply to 23884
XTOCb: it doesn't matter only for positive numbers. If Fix(x) is negative, then addition will increase it, and concatenation decrease.  :-)

Re: Well ... that's one way to Roundoff ...

2005-01-30 19:53 • by Foon

Sigh. See http://c2.com/cgi/wiki?FloatingPointCurrency and http://c2.com/cgi/wiki?FloatingPointFractions .


Currency should always be held in a fixed-point decimal. If you don't have that, store it as an integer holding the number-of-cents. And use variable names that remind you:

[code language="javascript"]

int UserPaymentCents

int PaidAmountMinutes[/code]

Re: Well ... that's one way to Roundoff ...

2005-04-08 11:50 • by Den Raskovalov

??/??


??????, ???? ????

Re: Well ... that's one way to Roundoff ...

2005-04-08 11:52 • by kvas
32448 in reply to 32447
????? ????, ???? ???!

Re: Well ... that's one way to Roundoff ...

2005-04-08 11:58 • by Den Raskovalov
Function roundoff2(ByVal x As Double)
    If x > 0 Then
        roundoff2 = Fix(x * 100 + 0.5) / 100
    Else
        roundoff2 = -Fix(-x * 100 + 0.5) / 100
    End If
End Function

Re: Well ... that's one way to Roundoff ...

2005-04-11 18:03 • by Maurits
32573 in reply to 32449
Anonymous:
Function roundoff2(ByVal x As Double)
    If x > 0 Then
        roundoff2 = Fix(x * 100 + 0.5) / 100
    Else
        roundoff2 = -Fix(-x * 100 + 0.5) / 100
    End If
End Function




I'm tempted to say...

Function roundoff2(ByVal x As Double)

    If x >= 0 Then

        roundoff2 = Fix(x * 100 + 0.5) / 100

    Else

        roundoff2 = -roundoff2(-x)

    End If

End Function

Re: Well ... that's one way to Roundoff ...

2009-05-20 07:43 • by Calculator Ftvb (unregistered)
I've done some programming in both VB and VB.Net (more in VB.Net), but given my knowledge of them, this function (or "WhatTheFunction" :-)) is just atrocious, and must have a better (shorter, easier, faster, etc.) alternative!

Re: Well ... that's one way to Roundoff ...

2010-02-17 17:08 • by Brian (unregistered)
This article was referenced by Raymond Chen in his discussion of this similar WTF code: http://blogs.msdn.com/oldnewthing/archive/2005/01/26/360797.aspx
« PrevPage 1Next »

Add Comment