简体   繁体   中英

XBase Language Rounding Weirdness

I have a requirement to take legacy code in an XBase-style language (FoxPro, in this case) and convert it to C#.NET. I want to get identical output from my rewrite, but rounding discrepancies are driving me nuts. I tried Decimal.Round(MidpointRounding) stuff alone, but found that I also needed to write my own "round up" function for midpoint = 5 situations.

That would be fine, if it worked, but what I'm seeing is that the my "round up" method is needed to agree with FoxPro output in some cases, but actually creates discrepancies in others. The database query is doing some multiplication and division, and I'm wondering whether math discrepancies between the two systems are accumulating.

Here is an example of what seems to me to be an inconsistency. The initial value of each pair comes from the output of my LINQ query. Given these, I want to generate a rounded result that is identical to the FoxPro result shown:

.NET LINQ Query result: 7.0477049103806673503597572093.
FoxPro rounding: 7.0

.NET LINQ: 7.2499596595318807183725770943.
FoxPro rounding: 7.3

Any suggestions as to how I can exactly duplicate the value from the legacy system without out resorting to a completely kludgey hack?

Your problem might have to do with floating point accuracy. Before the FoxPro program round, what type of variable does it store the value in ? And what type do you use ?

If the FoxPro program uses a floating point number with less accuracy than you do in C# (or vice versa), this might be the reason for the discrepancies you are seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM