views:

55

answers:

2

I am currently working on developing a program that stores Quantity as a decimal with four digit places for the very small minority of products that are sold in part (eg 100ml liquid being sold in variable amounts like 1mg-3.5mg).
I dislike this design as it causes problems for the entire program in deciding how to present the quantity and now some problems where there are products which incorrectly have decimal values.
Can you think of a better way to do this?
I'm looking for an alternative to a four decimal place field in the database.

The project uses SQL Server 2005.

Edit. I made this question more generic in order to obtain a larger potential audience

+1  A: 

In .NET, a System.Decimal is going to be your best option as System.Single and System.Double are not good at representing floating point numbers with accurate representational precision.

Can you provide a small, complete example that demonstrates your problem?

Andrew Hare
System.Decimal is your best bet C# side. Decimal is your best bet on SQL Server. Use a precision of 19 and a scale of 4. As for the formatting, there are a number of ways to handle management of the display. You might want to read up on IFormatProvider to consider for easily displaying your numbers. Also consider using an extension method that takes in a value of type decimal and a unit (e.g. ea, mg, etc.) and then appropriately adjusts the format. Extension method would be on System.Decimal. It might be used like System.Decimal.FormatMyData(Decimal input, string unit)
Anthony Gatlin
+2  A: 

Store the quantity in the smallest amount sold. So if something is sold by the mg, store it that way. That way you don't have to store fractions, and deal with rounding.

Just create a looking table for the product so that you can store the value of how it is stored and displayed, as well as any other meta-data about the size.

mrdenny