Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

float question Expand / Collapse
Author
Message
Posted Monday, December 17, 2012 10:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 14, 2013 4:15 AM
Points: 8, Visits: 9
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
Post #1397349
Posted Monday, December 17, 2012 11:03 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 10:05 AM
Points: 524, Visits: 1,159
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...
Post #1397353
Posted Monday, December 17, 2012 11:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 14, 2013 4:15 AM
Points: 8, Visits: 9
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
Post #1397361
Posted Monday, December 17, 2012 11:51 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 8:50 PM
Points: 1,688, Visits: 2,271
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)

Internet ATM Machine
Post #1397373
Posted Tuesday, December 18, 2012 1:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 14, 2013 4:15 AM
Points: 8, Visits: 9
> 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
Post #1397600
Posted Tuesday, December 18, 2012 6:47 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, January 16, 2013 4:23 PM
Points: 415, Visits: 2,333
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

Post #1397766
Posted Tuesday, December 18, 2012 8:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 14, 2013 4:15 AM
Points: 8, Visits: 9
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
Post #1397870
Posted Tuesday, December 18, 2012 9:25 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, January 16, 2013 4:23 PM
Points: 415, Visits: 2,333
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!


Post #1397878
Posted Tuesday, December 18, 2012 10:16 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 14, 2013 4:15 AM
Points: 8, Visits: 9
lol
Post #1397907
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse