-3

In this table I want to insert values in the weight column that is of float type with decimal values 00 like 100.00, 150.00, 25.00 .

In this table if I am going to insert 25.5 then it will show decimal value bit if I am storing 135.00 it will store 135 only not showing decimal if 00.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mahesh Chikhale
  • 73
  • 1
  • 10
  • 6
    Displaying of values is a function of the user interface layer, not the database. Is it purely display you are interested in, or are you more interested in the actual precision of the storage? – Colin Mackay Feb 02 '13 at 11:47
  • http://stackoverflow.com/questions/441600/write-a-number-with-two-decimal-places-sql-server/9313274#9313274 – HABO Feb 02 '13 at 14:21

2 Answers2

1

I'm not sure from your question what you are actually looking for. Storage and Display are two separate things.

STORAGE

If you want to store a number with a fixed precision in SQL SERVER use DECIMAL

CREATE TABLE MyTable
(
    MyNumber DECIMAL(5,2)
)

The above will store 5 digits, 3 before the decimal point, and two after.

Here's the documentation: http://msdn.microsoft.com/en-gb/library/ms187746.aspx

If you are storing currency values, then there is the MONEY datatype too: http://msdn.microsoft.com/en-us/library/ms179882.aspx

DISPLAY

If you are more interested in the display of values rather than the storage (You've already mentioned they are floats in the database) then in your C# application you can use something like this:

 string display = string.Format("{0:0.00}", myNumber);

Here's the documentation for custom formatting of numbers into strings: http://msdn.microsoft.com/en-gb/library/0c899ak8.aspx

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
  • For the display part: An alternative to `"0.00"` is `"F2"`, or just `"F"` if you want to rely on the number of decimals specified by the number format of the current culture. – Jeppe Stig Nielsen Feb 02 '13 at 12:00
0

If it is of type float, then X.00f = Xf where X is any integral number. Yes, it stores 135 or 135.00 - that is the same if it is a float, really.

poitroae
  • 21,129
  • 10
  • 63
  • 81