Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


float question


float question

Author
Message
veloopity
veloopity
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 14
sometimes float values are returned as "0E-7" instead of "0.0000000" or "4E-7" instead of "0.0000004" - is there some formatting statement or other method to prevent this?

-Michael
PiMané
PiMané
Mr or Mrs. 500
Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)

Group: General Forum Members
Points: 592 Visits: 1334
Nops.. that how SQL stores and retrieves the data...
If you don't want that behavior don't where floating point data types, use DECIMAL.
there's an article explaining why floating point numbers can be a problem...
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html.



If you need to work better, try working less...
veloopity
veloopity
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 14
thanks for the quick reply ... can I simply change the data types in the table design view? will the data that are already in there stay unharmed by this change?

-Michael
sgmunson
sgmunson
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2837 Visits: 3757
veloopity (12/17/2012)
thanks for the quick reply ... can I simply change the data types in the table design view? will the data that are already in there stay unharmed by this change?

-Michael


That depends on exactly what you change them to, and the existing values. For existing values that are integers, there's no loss of accuracy, but for any non-integer value, you can lose considerable precision if the number of decimal places you specify is small enough, and/or the level of accuracy needed is high enough. As an example, if you use a given field to represent an amount of money, and the smallest division of the money amount would result in at most 2 digits to the right of the decimal, AND, you know beyond all doubt that your data conforms, then you can use a SCALE of 2 without a problem. On the other hand, if you need to represent scientific data or measurements of some kind, moving away from float could have serious consequences for statistical measures or other group calculations. Unfortunately, you'll need to review the usage and representation for every field for which you contemplate a change.

Without considerably more detail, we'd have great difficulty providing much more than generalities... which may or may not apply to your situation.

Steve
(aka sgmunson)
:-):-):-)
Health & Nutrition
veloopity
veloopity
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 14
> sometimes float values are returned as "0E-7" instead of "0.0000000" or "4E-7" instead of "0.0000004"

what is the rule then ... when are numbers displayed like that and when not?

-m
patrickmcginnis59
patrickmcginnis59
SSC-Addicted
SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)

Group: General Forum Members
Points: 431 Visits: 2333
veloopity (12/18/2012)
> sometimes float values are returned as "0E-7" instead of "0.0000000" or "4E-7" instead of "0.0000004"

what is the rule then ... when are numbers displayed like that and when not?

-m


I don't know the rules, I suspect that if the number of places to either the left or right of the '.' exceed some limit, the various clients that display floating point from SQL servers will fall back to scientific notation. SSMS seems to be rather sensitive to places to the right of the decimal point, not so much to the left.

I like to use CONVERT for on the fly formatting, but check out some of the interesting results when using it. These not exactly right representations happen because you cannot represent some numbers exactly in internal binary (base 2) formats like floating point, that you could otherwise spec exactly in base 10 numbering systems.


CREATE TABLE #T (T1 FLOAT)
INSERT INTO #T (T1) SELECT 0.000001
INSERT INTO #T (T1) SELECT 0.00001
INSERT INTO #T (T1) SELECT 0.0001
INSERT INTO #T (T1) SELECT 0.001
INSERT INTO #T (T1) SELECT 0.01
INSERT INTO #T (T1) SELECT 0.1
INSERT INTO #T (T1) SELECT 1.
INSERT INTO #T (T1) SELECT 10
INSERT INTO #T (T1) SELECT 100
INSERT INTO #T (T1) SELECT 1000
INSERT INTO #T (T1) SELECT 10000
INSERT INTO #T (T1) SELECT 100000
INSERT INTO #T (T1) SELECT 1000000
INSERT INTO #T (T1) SELECT 10000000
INSERT INTO #T (T1) SELECT 100000000
INSERT INTO #T (T1) SELECT 1000000000
INSERT INTO #T (T1) SELECT 10000000000
INSERT INTO #T (T1) SELECT 100000000000
INSERT INTO #T (T1) SELECT 1000000000000
INSERT INTO #T (T1) SELECT 1.1
INSERT INTO #T (T1) SELECT 10.1
INSERT INTO #T (T1) SELECT 100.1
INSERT INTO #T (T1) SELECT 1000.1
INSERT INTO #T (T1) SELECT 10000.1
INSERT INTO #T (T1) SELECT 100000.1
INSERT INTO #T (T1) SELECT 1000000.1
INSERT INTO #T (T1) SELECT 10000000.1
INSERT INTO #T (T1) SELECT 100000000.1
INSERT INTO #T (T1) SELECT 1000000000.1
INSERT INTO #T (T1) SELECT 10000000000.1
INSERT INTO #T (T1) SELECT 100000000000.1
INSERT INTO #T (T1) SELECT 1000000000000.1

SELECT T1 FROM #T

SELECT CONVERT(NUMERIC(36,18),T1) FROM #T


veloopity
veloopity
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 14
interesting indeed! thanks for the little piece of code!

SELECT T1 FROM #T returns "1E-06" and "1E-05" for the first two lines (called from the SSMS)

-Michael
patrickmcginnis59
patrickmcginnis59
SSC-Addicted
SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)SSC-Addicted (431 reputation)

Group: General Forum Members
Points: 431 Visits: 2333
I was actually expecting to also see 1E+05, etc for the positive side of the exponent but nope, just straight digits! So I guess the rule is whatever the programmers felt like at the time!
veloopity
veloopity
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 14
lol
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search