benphp Posted February 21, 2012 Share Posted February 21, 2012 MS SQL Server - data type "real" I add a value of 95.4, and that's what displays in the SQL table, but when I fetch the value via PHP, I get 98.400002. Other values: 98.2 = 98.199997 98.3 = 98.300003 98.4 = 98.400002 98.5 = 98.5 98.6 = 98.599998 98.7 = 98.699997 98.8 = 98.800003 98.9 = 98.900002 WTF? I can use round to fix it, but would rather fix the problem than the symptom. Help? Thanks! Quote Link to comment Share on other sites More sharing options...
requinix Posted February 21, 2012 Share Posted February 21, 2012 REAL uses floating-point which means it cannot always give exact values. Use DECIMAL if you need exact values. Quote Link to comment Share on other sites More sharing options...
benphp Posted February 21, 2012 Author Share Posted February 21, 2012 But when I input exactly 98.8, it displays 98.8 in SQL Server Management Studio, but when I use PHP, it returns 98.800003. Why is it one value in one place and something else in another? It's not my database, or I'd change it. Quote Link to comment Share on other sites More sharing options...
kicken Posted February 22, 2012 Share Posted February 22, 2012 Why is it one value in one place and something else in another? Not all software treats floats the same way. Some software may use more or less bits to represent the value, or may have different levels of precision/rounding. There's a whole slew of problems/quirks one has to deal with when working with floating point values. That is why it is generally recommended you avoid them when possible. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.