Full precision output of floating point types in SQL Server Management Studio

旧巷老猫 提交于 2019-11-28 12:13:54

No.

SQL Server Management Studio rounds floating point values for display purposes; there is a Connect suggestion to change this behavior, but it is closed "as By Design".

However, SQLCMD, osql and the Query Analyzer do not.

SQLCMD -E -S server -Q"SELECT CONVERT(FLOAT, 1555.4899999999998)"

Is there a reason you would rather use a float type than a decimal type? Floats are stored as fractions, which causes them to often be slightly innacurate when doing operations on them. This is okay when you have a graphics application where the innaccuracy is much less significant than the size of a pixel, but it's a huge issue in something like an accounting application where you're dealing with money.

I would venture to say that the accuracy of a decimal is more important to most applications than any benefit in speed or size they would get from using a float.

Edit to my original post. I was trying to solve a slightly different problem when I ran across this page. I just wanted a float type to return a "regular" number, not scientific notation.

Suggested two converts originally. Wound up with this in then end. The 7 below could be changed to show as many decimal places as you would like. Replace and trims get rid of leading and trailing zeroes.

REPLACE(RTRIM(LTRIM(REPLACE(STR(x, 20, 7),'0',' '))),' ','0')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!