0

I have values stored as 0.05 and 0.10 in the database. I am creating a list using c# code to get these values in the list using :

foreach (var objST in obj.LstTerms)
{
     SubcriptionTermsModel objSubcriptionTerms = new SubcriptionTermsModel();
     objSubcriptionTerms.DiscountRate = objST.DiscountRate;
}

where DiscountRate is of type double. But when creating a list the value 0.10 is truncated as 0.1 which I am unable to compare on my view. Kindly help me with the solution for this.

  • `0.10` and `0.1` are numerically equivalent; there is no way of distinguishing between the two. Your issue probably arises when creating the string representation. You need to specify that you want two decimal digits; for example, using `num.ToString("F2")`. – Douglas Feb 24 '14 at 11:56
  • 1
    0.10 == 0.1, at least it was like this for last few hundred years. You are probably comparing strings on your view, put some more relevant code, including your table schema. – Anri Feb 24 '14 at 11:56
  • 1
    In fact `0.1` is not representable in `double`. The closest `double` to `0.1` is `0.10000 00000 00000 00555 11151 23125 78270 21181 58340 45410 15625`. I think you are going to need to recalibrate your expectations of what binary floating point can do for you. – David Heffernan Feb 24 '14 at 12:07

1 Answers1

0

As discussed here

double doesn't keep insignificant digits - there's no difference between 1.5 and 1.50000 as far as double is concerned.

If you want to preserve insignificant digits, use decimal instead. It may well be more appropriate for you anyway, depending on your exact context. (We have very little context to go on here...)

So you can use this decimal instead of double

Community
  • 1
  • 1
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61