English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a table with a float datatype. I create a record, and set the value of the field to .06, but the database stores it as 5.9999999999999998E-2. If I assign the value 1.06, it is stored as 1.0600000000000001. If I assign the value 110.06 to the field, it is stored as 110.06. How can I force the databse to retain only the precision I assign the field in my SQL, without modifying the schema, so that even small amounts like .06 are stored to two decimal points?

2007-01-05 11:55:16 · 6 answers · asked by © 2007. Sammy Z. 6 in Computers & Internet Programming & Design

I am not porting this to MySQL, when I say "my SQL," I am refering to my SQL statement.

2007-01-05 12:01:44 · update #1

As I said - changing the datatype is NOT an option.

2007-01-06 03:10:16 · update #2

6 answers

The previous answers were correct; you want to use the DECIMAL data type in SQL Server. Alternatively, you can use NUMERIC.

Both those types take a two-part argument: A precision and a scale. The precision is the total number of digits allowed, including decimal places; the scale is the number of decimal places you will want; in your case, 2.

So, DECIMAL(10,2) or NUMERIC(10,2) would allow any number from 0.00 to 99999999.99.

ALTER TABLE tablename
ALTER COLUMN columnname
DECIMAL(10,2)

UPDATE:

If changing the data type is not an option, the best you can do is round your results in select statements.

SELECT ROUND(column, 2) FROM table

http://msdn2.microsoft.com/en-us/library/ms175003.aspx

PS: Try not to be so curt. We're trying to help you; copping an attitiude isn't the appropriate response when you are asking for favors.

2007-01-05 14:59:05 · answer #1 · answered by Anonymous · 0 2

If you can't change the data type for the column, you can't change how it is stored by the database manager. Float values are stored using the IEEE standard for floating point numbers, which has this behavior (not really a bug, though it is bugging you).

As a workaround, try using a CAST when you select from the table, for example

select cast(float_column as decimal(4,2)) from table

The DBMS will round the number, and you'll get the output you want.

For all the ugly details on floating point numbers, see the Wikipedia article below.

2007-01-12 07:42:06 · answer #2 · answered by Anonymous · 0 0

Create Table Float

2016-12-18 05:45:55 · answer #3 · answered by bridson 4 · 0 0

As the other poster stated, use Decimal. The 2 fields you need to be aware of are Precision and Scale.

Precision specifies the number of digits to the left of the decimal and Scale is the number of digits to the right of the decimal. It has its limitations in this respect since it helps to know your max values ahead of time.

2007-01-05 14:08:32 · answer #4 · answered by Scottee25 4 · 1 0

http://www.databasejournal.com/features/mssql/article.phpr/2212141

FLOAT does rounding. Use DECIMAL.

2007-01-05 12:00:54 · answer #5 · answered by Anonymous · 0 0

You have to use a program to encrypt and it will be interesting.

2016-03-17 23:11:34 · answer #6 · answered by Anonymous · 0 0

fedest.com, questions and answers