What is a decimal?

# Thread: What is a decimal?

1. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## What is a decimal?

Hi,<BR><BR>The SQL Server manual says:<BR><BR>&#039;The decimal data type stores an exact representation of the number; there is no approximation of the stored value.&#039;<BR><BR>What exactly does this mean?<BR><BR>The following script...<BR><BR>declare @dec1 as decimal<BR>set @dec1 = 10/3<BR>print @dec1<BR><BR>gives an output of &#039;3&#039;! That doesn&#039;t seem very exact to me.<BR><BR>Cheers,<BR><BR>I.

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: What is a decimal?

decimal takes two values if you read on number of integer and number of &#039;decimal&#039; unless you declare them it defaults to integer only!<BR><BR>So<BR><BR>declare @dec1 as decimal(5,5) &#039; as an example

3. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## RE: What is a decimal?

It has evaluated the division of two integers and given you the integer result. If you want to see a more exact answer, try 10.0/3.0.

4. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## And...

see WK&#039;s reply as well. DECIMAL by itself won&#039;t do you much good.

5. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## RE: And...

Cheers for the reply.<BR><BR>declare @dec1 as decimal(5,5)<BR>set @dec1 = 10/3<BR>print @dec1<BR><BR>Now I get...<BR><BR>&#039;Arithmetic overflow error converting numeric to data type numeric.&#039;<BR><BR>So if a user tries to enter &#039;10/3&#039; into the database, I need to put up a request asking them to try a value which doesn&#039;t have a recurring decimal?!<BR><BR>

6. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## The second number...

cannot be the same or larger than the first. Lookup scale and precision in the documentation. Try DECIMAL(10, 5) or even DECIMAL(6,5). Also, you still need 10.0/3.0 or you&#039;ll still get 3 (with more decimal points at the end).

7. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## RE: The second number...

OK, so now I&#039;m getting somewhere.<BR><BR>declare @dec1 as decimal(30,25)<BR>set @dec1 = 10.0/3.0<BR>print @dec1<BR><BR>This gives 3.3333330000000000000000000!<BR><BR><BR>Anyhow, what is the difference between a decimal&#039;s &#039;an exact representation of the number&#039; and float and real&#039;s approximation of a number?<BR><BR>Cheers,<BR><BR>I.

8. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## RE: The second number...

From Books OnLine (check out the first sentence especially):<BR><BR>Approximate numeric data types do not store the exact values specified for many numbers; they store an extremely close approximation of the value. For many applications, the tiny difference between the specified value and the stored approximation is not noticeable. At times, though, the difference becomes noticeable. Because of the approximate nature of the float and real data types, do not use these data types when exact numeric behavior is required, such as in financial applications, in operations involving rounding, or in equality checks. Instead, use the integer, decimal, money, or smallmoney data types.

9. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## RE: The second number...

Yeah I read that but it doesn&#039;t make all that much sense to me that&#039;s all.

10. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## profound

You see, in modern mathematics, &#039;an extremely close approximation&#039; doesn&#039;t mean a whole lot by itself.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•