I'm using VB.NET, Visual Studio 2013.
I'm writing a helper utility to export data from a database containing invoicing information. That data comes from another program that generates the invoices and such. I didn't write the invoice program, and I cannot change it.
Basically, my program creates a simple CSV export that the user then uploads to a web portal (it's essentially an "invoice verification system" that compares the physical invoice received by a customer with the data in the invoice database). If the total invoice value is off even by a penny, the system rejects it, and they have to go back and manually verify, which is understandably annoying.
My problem is that when I get the data from the invoicing database, I have to round that data, and all too often I'm off by a penny. For example, I have this number:
The invoicing program represents the value of that number as 26.01. My program rounds it to 26.02.
I've tried the various flavors of Math.Round, and each time it rounds to 26.02 (as I kinda expect). I've read up on the Math.Round MSDN article, so please don't link to that - it doesn't help, and in fact it seems Math.Round is doing what it's supposed to do.
I've tried the function below, which I harvested from the invoice program's DLL file (after deconstructing it, and converting it to VB.NET):
Dim flag As Boolean
If dblNumber < 0.0 Then
flag = True
flag = False
Dim num As Double = Math.Pow(10.0, CDbl(intDecimals))
Dim num2 As Double = (Math.Abs(dblNumber) * num) + 0.5
Dim num3 As Double = Conversion.Int(Conversions.ToDouble("" & Conversions.ToString(num2))) / num
If flag Then
num3 *= -1.0
Even with that - which is the exact code they have, converted to VB.NET - I still get 26.02.
I've tried other Rounding functions I gathered from the web, and they all seem to return 26.02. The only way I've been able to get 26.02 is to convert the value to a string and grab the 2 characters immediately after the decimal point. While this works, it seems as if there should be a way to convert it using numeric values.
Any help would be much appreciated.
BTW, here's the code from the invoicing program, exactly as written:
public double Round2(double dblNumber, short intDecimals)
if (dblNumber < 0.0)
flag = true;
flag = false;
double num = Math.Pow(10.0, (double) intDecimals);
double num2 = (Math.Abs(dblNumber) * num) + 0.5;
double num3 = Conversion.Int(Conversions.ToDouble("" + Conversions.ToString(num2))) / num;
num3 *= -1.0;