I need to be able to store numbers like 3.5 in my table. So I've used the decimal type field. But if I enter 3.5 it round it up to 4. Am I being stupid or is it not the point of decimal to have a decimal point?
The default scale of a decimal is zero, which means that what you have is actually an integer type. So, it's not possible to return the value 0.5 without changing the data type. If you specify a precision and scale for the type, like decimal(18,2) , it can handle numbers that are not integers.
SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places. Tip: Also look at the FLOOR() and CEILING() functions.
The Basic syntax of Decimal data type in SQL Server Where, p stands for Precision, the total number of digits in the value, i.e. on both sides of the decimal point. s stands for Scale, number of digits after the decimal point.
SQL ROUND Function You might have known CEILING and FLOOR, but ROUND is by far the most common. Rounding just means to round up from 5 or down from anything less. ROUND is unique because you can tell SQL which position you would like rounded.
You need to declare it like decimal(18,3) to specify the number of digits after the point.
In case you are using stored procedure parameter also must have precision specified next to decimal, e.g. decimal(18,3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With