Hi, i am working on an inventory application.
i am stuck at the unit conversion , like if a product is in Kilogram, i need to sell in grams or tons
, dozen to pcs and custom pack is also there like a carton may contain 20 pcs and need to sell it in pcs or it may contain 40 pcs. i hope you understand my question,
Product table is like
ProductID Name Rate Unit Baseunit conversion
1 Item A 100 carton Pcs 20
2 item B 120 carton Doz 10
3 item C 100 Kilogram Gram 2
ex: Item A is in carton of 20 pcs
item C is a bag of 2 kilogram
and there is another table for measuring Unit
Unit_id Unit_Name Parent_ID Conversion
1 Pcs - 1
2 Doz 1 12
3 gram - 1
4 Kilogram 3 1000
like wise
What I have tried:
i tried but didn't get a complete solution for this, please help
this is what i tried
public enum Units
{
Grams = 1,
KiloGrams = 2,
Pounds = 3,
Metres = 101,
Millimetres = 102,
Centimeters = 103,
Feet = 106,
Inches = 107,
Litres = 201,
Millilitres = 202,
FluidOunces = 204,
Numbers = 401,
Dozen = 402,
Carton = 400
}
class UnitConverter
{
public static double CtnCount = 0;
const int MassRange = 0;
const int LengthRange = 100;
const int VolumeRange = 200;
const int Quantity = 400;
static double[] ToBaseWeight = new double[7];
static double[] ToBaseLength = new double[8];
static double[] ToBaseVolume = new double[10];
static double[] ToBaseQty = new double[7];
static UnitConverter()
{
ToBaseWeight[(int)Units.Grams] = 1;
ToBaseWeight[(int)Units.KiloGrams] = 1000;
ToBaseWeight[(int)Units.Pounds] = 453.59237;
ToBaseLength[(int)Units.Metres - LengthRange] = 1;
ToBaseLength[(int)Units.Millimetres - LengthRange] = 0.001;
ToBaseLength[(int)Units.Centimeters - LengthRange] = 0.01;
ToBaseLength[(int)Units.Feet - LengthRange] = 0.3048;
ToBaseLength[(int)Units.Inches - LengthRange] = 0.0254;
ToBaseVolume[(int)Units.Litres - VolumeRange] = 1000;
ToBaseVolume[(int)Units.Millilitres - VolumeRange] = 1;
ToBaseVolume[(int)Units.FluidOunces - VolumeRange] = 29.5735296;
ToBaseQty[(int)Units.Numbers - Quantity] = 1;
ToBaseQty[(int)Units.Dozen - Quantity] = 12;
ToBaseQty[(int)Units.Carton - Quantity] = 100;
}
public static string ConvertUnits(double amount, Units from, Units to,double cnt)
{
CtnCount = cnt;
if (from == to) return amount.ToString();
string msg = ValidateUnitMatch(from, to);
if (msg != "")
{
return msg;
}
int range = GetConversionRange(from);
double[] rateArray = GetConversionArray(range);
double answer = 0;
if (CtnCount>0)
{
rateArray[0] = CtnCount;
}
answer = rateArray[(int)from - range] * amount;
answer = answer * (1.0 / rateArray[(int)to - range]);
return answer.ToString();
}
static string ValidateUnitMatch(Units from, Units to)
{
int frange = ((((int)from) / 100) * 100);
int trange = ((((int)to) / 100) * 100);
if (frange != trange)
{
return "The from unit and to unit are not compatible. They need to be of the same type (Volume/Area/Mass/Length)";
}
else
{
return "";
}
}
static int GetConversionRange(Units unit)
{
int range = ((((int)unit) / 100) * 100);
return range;
}
static double[] GetConversionArray(int range)
{
switch (range)
{
case VolumeRange: return ToBaseVolume;
case LengthRange: return ToBaseLength;
case MassRange: return ToBaseWeight;
case Quantity: return ToBaseQty;
}
throw new ArgumentOutOfRangeException("Unknown unit type");
}
}
its working but i was not able to use custom unit.. like i said above
if i want to add a new unit like a bag contains 10KG item i cannot do it in this.