简体   繁体   中英

How to divide a decimal number in C#?

I am developing a POS application for a medical store using C#. As per the store requirements, they have two types of products.

  1. Products without variations (Do not have sub-products ie A bottle of suspension or baby food).
  2. Products with variations (Have sub-products ie A Packet of tablets-> Have 3 Strips -> Each strip contain 10 tablets)

For products with variations, I have the variation standard (Every product has a specific quantity of strips within each packet and each strip has a specific amount of tablets). I have managed them with levels. ie Level A (For parent product), Level B (For sub-item eg strips within a packet), Level C (For single tablet within a strip). I have managed products with variations like this in order to speed up the sale-out operation.

According to customer behavior, I want to manage the sale operation such that if a customer purchases a packet of tablets and the packet contain 3 strips and each strip further contain 10 tablets, I want to fully capture this stock reduction in my database.

I have a separate table in my SQLServer for this purpose. The table contains both products (with and without variations). The table also contains a separate field called IsParent for tracking products. The IsParent field stores 0 for Products without Variations and 1 for Products with Variations. The table also contains separate fields for each Level of a variation based product and a separate quantity field for each level.

Suppose I have 10 packs of XYZ tablets and it's Level B Standard is 3 (3 strips in each pack) and it's Level C standard is 10 (10 tablets in each strip), then my existing stock will be something like this:

10 X 3 = 30 strips

30 X 10 = 300 tablets

Now, if a customer purchases a single strip out of those 10 packets then how can I subtract 1 strip from the whole stalk ie 30 strips? And how can I subtract those 10 tablets (1 strip) from the whole stalk ie 300 tablets?

Until now, I am doing the following manual subtraction:

For Level B (a single or two strips each having 10 tablets)

decimal levelB = purchaseQuantity / levelBStandard;
decimal levelB_Qty = Math.Round((Decimal)levelB, 1);

The purchase quantity is the quantity that a customer has purchased. This quantity is divided by the level B standard ie 3. If a customer purchase 2 strips, then 2/3 = 0.66 and if a customer purchase 1 strip, then 1/3 = 0.33 . If another customer will purchase a single or two strips from the same product, then the stock amount will go tricky after a few transactions due to the decimal point.

I am also doing the same operation if a customer will purchase less than 10 tablets which are quite tricky for me. I need some way to manage this stock reduction such that if 2 strips are sold out from 30 strips in one sale operation, then the remaining stock quantity will be 29.34 or 29.3 . Later, in another sale operation of the same product, if another customer purchase a single strip, then the stock amount of strips should become 29 instead of 29.01 because this 0.1 is creating problems for me.

I will be managing the stock reduction for tablets in the same way as well. I hope somebody may help me with a simple but reliable solution. Any help will be extremely appreciated.

Inventory management systems typically deal with these sorts of issues by abstracting the concept of a "product" (ie something you sell) from a "unit" (ie something you keep inventory).

In your case, you would manage inventory by units of tablets or strips (whatever the applicable atomic representation of your inventory is), and then create products related to those inventory units.

A product might have a 1:1 relationship with an inventory unit (eg the product "1 strip of XYZ (10 tablets)" = 1 x strip in inventory), or a product might be a bundle (eg the product "Pack of XYZ (30 tablets)" = 3 x strip in inventory).

Your inventory costing is managed using whatever method you choose (FIFO, weighted average, etc), and your products each have their own (or multiple) price points.

When the product "Pack of XYZ (30 tablets)" is sold, your inventory goes down by 3 x "strip" units (and their associated cost-of-goods-sold is calculated) and your revenue goes up by whatever the price of the "Pack of XYZ (30 tablets)" product was sold at.

I Try To Divide A Decimal Number in C# And I Get the Output.. that show below

namespace ConsoleApp1
{
   class Program
   {
      static void Main(String[] args)
      {
        Decimal dividend = 1234.0m;
        Decimal divisor = 21.1m;`
        for(int ctr = 0; ctr <= 10; ctr++)
        {
           Console.WriteLine("{0:N1} / {1:N1} = {2:N4}", dividend, divisor, Decimal.Divide(dividend, divisor));
           dividend += .1m;
        }
        Console.ReadKey();
      }
   }
}

And I Get Output Is Show Below :-

1,234.0 / 21.1 = 58.4834
1,234.1 / 21.1 = 58.4882
1,234.2 / 21.1 = 58.4929
1,234.3 / 21.1 = 58.4976
1,234.4 / 21.1 = 58.5024
1,234.5 / 21.1 = 58.5071
1,234.6 / 21.1 = 58.5118
1,234.7 / 21.1 = 58.5166
1,234.8 / 21.1 = 58.5213
1,234.9 / 21.1 = 58.5261
1,235.0 / 21.1 = 58.5308

I think that is useful for you... Thank You....

EDIT : My Suggestion is to avoid decimal if unit is not a weighted unit and manage the rest as following

Considering the complexity of work and the efforts you have already put in to this I would recommend to do little architectural changes.

That is you should classify your "products with variations" into two different types "Packed" and "detached" and manage them differently.

Now for example there is a product with variation name as "Panadol". 1 pack of panadol (Parant Product) contains 60 tablets (Child Product).

You have an inventory coming in of product "panadol" 10 packs and 30 tablets, you will associate them with same batch number.

Now your inventory should look like this

//Inventory table 

| InventoryID | Batch |  ProductID  |
|    1        | 001   |    1        | 

// Stock table 
| StockID | InventoryID  |   Packed Stock | Detached Stock |  
|    1    |      1       |       10       |        30      |

First Transaction 1 pack, you need to check inventory then make transaction your table will look like this

// Stock table 
| StockID | InventoryID  |   Packed Stock | Detached Stock |  
|    1    |      1       |       9        |        30      |   

Second Transaction 20 tablets , you need to check if inventory then make transaction your table will look like this

// Stock table
| StockID | InventoryID  |   Packed Stock | Detached Stock |  
|    1    |      1       |       9        |        10      | 

Third Transaction 65 tablets , First you will check detached stock, ie not enough. Then You will check packed stock, ie enough Then there must be META DATA table which will keep the record that 1 panadol pack contains 60 tablets. You Will Now move packed stock to detached stock. Your table must look like this

   // Stock table
    | StockID | InventoryID  |   Packed Stock | Detached Stock |  
    |    1    |      1       |       8        |        70      |  

Now you will Make transaction and your table will look like this

// Stock table
| StockID | InventoryID  |   Packed Stock | Detached Stock |  
|    1    |      1       |       8        |        5       |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM