3 Replies - 372 Views - Last Post: 13 February 2020 - 02:18 PM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

C# API returning many decimal places after 2

Posted 13 February 2020 - 10:10 AM

Hi everyone, I am a little confused on why I am getting a double/decimal value that is returning more than 2-4 decimals places.

In the SQL database, it is restricted to 4 decimal places. In my code, I make a select statement and I tested it in the SSMS as well and verified that it returns only 2-4 digits after the decimal place.

In my code, I have to convert it to a double using methods like this

while(reader.Read())
{
        // I did not have Math.Roun() before, so I tried using it here and it's still giving me an issue
	lastPayment.LastPaymentAmount += Math.Round(Convert.ToDouble(reader["Amount"].ToString()), 2);
	lastPayment.LastPaymentDate = Convert.ToDateTime(reader["dtcshrcv"].ToString());
	if(reader["sdettycd"].ToString() == "LTE" || reader["sdettycd"].ToString() == "LTC")
		lastPayment.FeeLate += Math.Round(Convert.ToDouble(reader["Amount"].ToString()), 2);
	if (reader["sdettycd"].ToString() == "OTH")
		lastPayment.FeeOther = Math.Round(Convert.ToDouble(reader["Amount"].ToString()), 2);
	if (reader["sdettycd"].ToString() == "TAX")
		lastPayment.FeeTax = Math.Round(Convert.ToDouble(reader["Amount"].ToString()), 2);
}



When the object returns, I get a number like this 100.8500000000000002

or I get something like
-2.842170943040101e-14

And I do not know why this is happening when I am rounding to 2 decimals.

Has anyone run into something like this before?

Is This A Good Question/Topic? 0
  • +

Replies To: C# API returning many decimal places after 2

#2 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Re: C# API returning many decimal places after 2

Posted 13 February 2020 - 10:27 AM

I found a fix and changed it to doing it this way

lastPayment.LastPaymentAmount = Math.Round(Convert.ToDouble(reader["Amount"].ToString()) + lastPayment.LastPaymentAmount, 2);



Something was happening with the addition and I am not sure what the reason is.

It did not like it when I did the addition so I got rid of the += and just added the last amount to the next amount to get it and rounded that to 2 decimal places.

I'm guessing that it has to do with the values being a double data type.

This post has been edited by fearfulsc2: 13 February 2020 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5526
  • View blog
  • Posts: 14,527
  • Joined: 18-April 07

Re: C# API returning many decimal places after 2

Posted 13 February 2020 - 11:48 AM

Yeah usually computers try to hold on to as much precision as they can when it comes to decimals. So if you take an integer and mix it with a double, it is going to give you the double so that it won't lose the data in the decimal. So you probably need to take a look at the data types.

But one thing I wanted to mention to you is that if you are dealing with money, you really should be using "decimal" data types and not doubles. If you are interested, you can read more about it here...

https://docs.microso...he-decimal-type

Glad things are working out for you. :)
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: C# API returning many decimal places after 2

Posted 13 February 2020 - 02:18 PM

Personally, I would have written your code in post #1 into something like:
decimal lastPaymentAmount = lastPayment.LastPaymentAmount;
while(reader.Read())
{
    lastPaymentAmount += Decimal.Parse(reader["Amount"].ToString());

    lastPayment.LastPaymentDate = DateTime.Parse(reader["dtcshrcv"].ToString());

    decimal amount = Math.Round(Decimal.Parse(reader["Amount"].ToString()), 2);
    switch (reader["sdettycd"].ToString())
    {
    case "LTE":
    case "LTC":
        lastPayment.FeeLate += amount;
        break;

    case "OTH":
        lastPayment.FeeOther = amount;
        break;

    case "TAX":
	lastPayment.FeeTax = amount;
        break;
    }
}
lastPayment.LastPaymentAmount = Math.Round(lastPaymentAmount, 2);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1