SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


FLOAT vs DECIMAL..


FLOAT vs DECIMAL..

Author
Message
PiMané
PiMané
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2896 Visits: 1351
Hi,

I've a table that stores currency amount values and is used in some calculations (vat, discounts, ....).
FLOAT, REAL, MONEY and SMALLMONEY can have round issues due to their internal storage method in SQL Server being DECIMAL the only one that hasn't that issue.
What's the best mapping from each type to DECIMAL?
Should FLOAT and MONEY use the default DECIMAL precision (18) and REAL and SMALLMONEY 9 ?

Thanks,
Pedro



If you need to work better, try working less...
samalex
samalex
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1159 Visits: 1068
Technically FLOAT, REAL, MONEY and SMALLMONEY all have different ranges, but my standard is just to convert all numeric values of different types, especially values with currency, to Numeric(17,2), and for percents I use Numeric(17,6). Doing this keeps all numeric values standard across the database.

Also Numeric and Decimal are equivalents, I just have always used Numeric, but you can use Decimal just the same.

Hope this helps ..

Sam
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14389 Visits: 11848
samalex (12/20/2012)
Technically FLOAT, REAL, MONEY and SMALLMONEY all have different ranges, but my standard is just to convert all numeric values of different types, especially values with currency, to Numeric(17,2), and for percents I use Numeric(17,6). Doing this keeps all numeric values standard across the database.

Also Numeric and Decimal are equivalents, I just have always used Numeric, but you can use Decimal just the same.

Hope this helps ..

Sam


You could use NUMERIC(19,2) and NUMERIC(19,6) as your standards, since they use the same amount of storage, 9 bytes, as NUMERIC(17,2) and NUMERIC(17,6).
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39430 Visits: 14411
Comparing FLOAT and REAL to DECIMAL and NUMERIC is not really fair. The former are approximate numeric data types and the latter are exact numeric data types. I am leaving MONEY and SMALLMONEY out of it because those should never be used. For example if you're currently using FLOAT and REAL to sum the precent-segments of a set where the percentage sum should equal 100.00 then if you switch to an exact data type you will start having issues, e.g. coming up with 99.76 or some non-trivial amount away from 100.00. Just throwing it out there, be sure you test to ensure you have chosen the right data type for the job.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
PiMané
PiMané
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2896 Visits: 1351
opc.three (12/21/2012)
Comparing FLOAT and REAL to DECIMAL and NUMERIC is not really fair. The former are approximate numeric data types and the latter are exact numeric data types. I am leaving MONEY and SMALLMONEY out of it because those should never be used. For example if you're currently using FLOAT and REAL to sum the precent-segments of a set where the percentage sum should equal 100.00 then if you switch to an exact data type you will start having issues, e.g. coming up with 99.76 or some non-trivial amount away from 100.00. Just throwing it out there, be sure you test to ensure you have chosen the right data type for the job.


My main problem wanting to change to DECIMAL is that we deal with money values and store the value with out VAT, therefore it can be a long decimal value...
But cause we store it in float the invoice report, occasionally, gives different values for the total since each line is VALUE + VAT and the total is SUM(VALUE) + VAT and sometimes they are different due to the floating point issue, and also we had 2 reports with different values (cents but different...).

Thanks,
Pedro



If you need to work better, try working less...
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39430 Visits: 14411
For $ I would recommend an exact numeric data type with the scale you need to satisfy your accounting requirements.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
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