0

I have a donations webpage where people can specify what percentage of their donation goes to what charity. I have validation in place to ensure that they cannot allocate more than 100%. This works fine until people add decimal values.

The percentages are stored on a SQL database as floats, and I bring in the total percentage available to allocate using the following stored procedure.

SELECT giftid, SUM(percentage) totalPercentage 
FROM dbo.giftFundArea
WHERE giftid = @giftid
GROUP BY giftid

My page looks at the stored procedure and accesses the value using the following code.

Dim percentageAllocated As Double = myReader.GetDouble(myReader.GetOrdinal("totalPercentage"))

When I run the stored procedure in Managememt Studio I see that the total percentage allocated for a particular gift is 33.4% but when I debug my page I see that percentageAllocated has a value of 33.400000000000006%.

This is obviously throwing off my validation.

Can anyone suggest how I stop this incorrect conversion?

Thanks in advance.

Simon
  • 1,293
  • 5
  • 21
  • 39
  • Why use float in the db in first place? See:http://stackoverflow.com/questions/1056323/difference-between-numeric-float-and-decimal-in-sql-server – NoChance Jan 19 '15 at 13:32

1 Answers1

2

Floating point values are intrinsically susceptible to rounding errors. Use a Decimal instead that doesn't have this issue:

Dim percentageAllocated As Decimal = myReader.GetDecimal(myReader.GetOrdinal("totalPercentage"))
Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143