Oracle float (B)

Source: Internet
Author: User

From: http://www.cnblogs.com/boulder19830907/archive/2008/01/31/1059627.html

Someone asked me today, after designing the field type to float (2) and inserting 93.5 data, why is it 90?

In order to illustrate this problem, let's look at a paragraph (http://www.cnoug.org/viewthread.php? Tid = 56643): Oracle online help says: Float (B) specifies a floating-point number with binary precision B. The precision
B can range from 1 to 126. to convert from binary to decimal precision, multiply B by 0.30103.

Based on this section, we can see that 2 in float (2) is a binary precision, rather than the commonly used decimal precision. The Conversion Relationship between them is: Binary precision = int (B * 0.30103), so the actual precision here should be equal to int (2*0.30103) = 0, that is, the precision after the decimal point is 0.

Back to our original problem, 93.5 is converted to floating point 9.35*10 ^ 1. At this time, after 9.35 decimal points, the precision is 0 to 9, and then it is changed to 9*10 ^ 1 = 90.

Similarly, we can design it as float (10), so int (10*0.30103) = 3. Therefore, if we insert 93.5, we will get 9.35 -- the precision is 3 --> 9.350, 9.350*10 ^ 1 = 93.5. If 13884.2 is inserted, 1.38842 -- the precision is 3 --> 1.388, 1.388*10 ^ 4 = 13880.

We can see this conversion rule more clearly from the following tests.

Connected to Oracle9i Enterprise Edition Release 9.0.1.1.1
Connected as aspire

SQL>
SQL> Create Table hjm_float_test
2 (a float (2 ),
3 B float (10 ),
4 C float,
5 d number );

Table created

SQL> insert into hjm_float_test (A, B, C, D) values (93.5, 93.5, 93.5, 93.5 );

1 row inserted

SQL> insert into hjm_float_test (A, B, C, D) values (93.665, 93.665, 93.665, 93.665 );

1 row inserted

SQL> insert into hjm_float_test (A, B, C, D) values (96.5, 96.5, 96.5, 96.5 );

1 row inserted

SQL> insert into hjm_float_test (A, B, C, D) values (13884.2, 13884.2, 13884.2, 13884.2 );

1 row inserted

SQL> commit;

Commit complete

SQL> select * From hjm_float_test;

A B c d
----------------------------------------------------------------------------------------------------------
90 93.5 93.5 93.5
90 93.67 93.665 93.665
100 96.5 96.5 96.5
1000 13880 13884.2 13884.2

SQL> DESC hjm_float_test;
Name type nullable default comments
---------------------------------
A float y
B float y
C float y
D Number y

SQL>

Please note that, although I designed the tables as float (2) and float (10), when I was in the DESC table (the last 7 columns of the Code) you cannot see such a design structure. Special attention should be paid to such problems !!

Finally, let's review,Remember that B in float (B) is the binary precision, and its conversion rule is binary precision = int (B * 0.30103 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.