views:

587

answers:

2

I have a column DECIMAL(9,6) i.e. it supports values like 999,123456.

But when I insert data like 123,4567 it becomes 123,456700

How to remove those zeros?

+6  A: 

A decimal(9,6) stores 6 digits on the right side of the comma. Whether to display trailing zeroes or not is a formatting decision, usually implemented on the client side.

But since SSMS formats float without trailing zeros, you can remove trailing zeroes by casting the decimal to a float:

select 
    cast(123.4567 as DECIMAL(9,6))
,   cast(cast(123.4567 as DECIMAL(9,6)) as float)

prints:

123.456700  123,4567

(My decimal separator is a comma, yet SSMS formats decimal with a dot. Apparently a known issue.)

Andomar
+1 I thought the conversion to float would introduce some imprecision to the results but it appears to work absolutely fine.
Martin Smith
+2  A: 

Unless you want to convert it to a string you can't. You need to do this in the presentation layer.

Martin Smith
How can I do that in presentation if it is a column in asp:GridView? What format have I to apply? TIA
abatishchev
Not sure if there is a better way but a solution is here http://forums.asp.net/t/1114891.aspx
Martin Smith
Yea, I saw that topic but resulting string of `{0:F2}` is `30,50` instead of `30,495600` or `30,4956`
abatishchev
It was ggorczow's answer on that thread I was referring to.
Martin Smith