Product Unit of Measure Conversion Pseudocode

Unit of Measure Conversion Pseudocode

I don’t know if we will have the same term in mind, but here in our country we call it Product Unit of Measure or UOM. For example, we have a “candy” product. Possible unit of measures could be: pieces, packs and boxes.

The purpose of this post is to solve the problem of converting product quantity or inventory values in different unit of measures. I think SAP has this functionality. Just in case you want to have the same in your current program, keep on reading!

The following questions might give you some more ideas about this post:

How many box are 132 pieces?
How many box are 309 packs?
How much of a box are 33 pieces?
How much of a box are 5 packs?

…and many other combinations. So let’s get started!

Contents

Example Product

For example we have a product with 3 unit of measures.

Product code: COF_A1
UOMs: box, pack, pcs

Three Objects to Consider

In this post, when I say “upload”, it generally refers to saving the value in the system database. Anyways, when working with unit of measure conversion, the user usually deals with three objects:

1. Product (above)
2. Unit of Measures (UOM)
3. UOM Conversion Values

Uploading Unit of Measures (UOM)

Tell the user to upload their UOMs from biggest to smallest. It will look like this in the list or CSV:

COF_A1,box
COF_A1,pack
COF_A1,pcs

Uploading UOM Conversion Values

When uploading UOM conversion, the bigger UOM/s are on the left side and the smallest UOM is always on the right side. For example:

1 box = 24 pcs
1 pack = 12 pcs

UOM Conversion in Action (Examples)

Now apply it in the following input based on the above UOM conversion.

Convert the following pcs to box:

8 pcs = ? box:

First, convert to smallest UOM:

8 pcs = 8 pcs (since pcs is already the smallest UOM, no need to multiply to any value)

Second, divide the smallest UOM to “box” conversion value:

8 / [24] = [0.34 box]

Final answer:

8 pcs = 0.34 box

Convert the following pcs to pack:

15 pcs = ? pack:

Convert to smallest UOM:

15 pcs = 15 pcs (since pcs is already the smallest UOM)

Divide the smallest UOM to “pack” conversion value:

15 / [12] = [1.25 pack]

Final answer:

15 pcs = 1.25 pack:

Convert the following packs to pcs

9 pack = ? pcs:

Convert to smallest UOM.

9 pack = 9 * [12] = [108 pcs]

Final answer. No need to divide to a value since we already get the pcs value

9 pack = 108 pcs

Convert the following pack to box

11 pack = ? box:

Convert to smallest UOM.

11 pack = 11 * [12] = [132 pcs]

Divide the smallest UOM to “box” conversion value:

132 / [24] = [5.5 box]

Final answer.

11 pack = 5.5 box

Convert the following box to pcs

3 box = ? pcs:

Convert to smallest UOM.

11 box = 11 * [24] = [264 pcs]

Final answer. No need to divide to some value since we got the “pcs” value already.

11 box = 264 pcs

Convert the following box to pack

7 box = ? pack:

Convert to smallest UOM. Multiply by “box” conversion value.

7 box = 7 * [24] = [168 pcs]

Divide the smallest UOM to “pack” conversion value:

168 / [12] = [14 pack]

Final answer.

7 box = 14 pack

Convert the following “box and pack” to pcs

2 box and 8 pack = ? pcs

First, convert them to smallest UOM:

2 box = 2 * [24] = [48] pcs
8 pack = 8 * [12] = [96] pcs

Second, add them:

48 pcs + 96 pcs = [144 pcs]

Final answer. No need to divide some values since pcs is the smallest UOM.

2 box and 8 pack = 144 pcs

Convert the following “box and pcs” to pack

4 box and 18 pcs = ? pack

Convert them to smallest UOM:

4 box = 4 * [24] = [96] pcs
18 pcs = [18] pcs (as is since pcs is the smallest UOM)

Add them:

96 + 18 = [114 pcs]

Divide the total smallest UOM to “pack” conversion value:

114 pcs / [12] = [9.5 pack] (yes, we divide this time!)

Final answer:

4 box and 18 pcs = 9.5 pack

Convert the following “pack and pcs” to box

3 pack and 13 pcs = ? box

Convert them to smallest UOM:

3 pack = 3 * [12] = 36 pcs
13 pcs = [13] pcs (as is since pcs is the smallest UOM)

Add them:

36 pcs + 13 pcs = 49 pcs

Divide the total smallest UOM to “box” conversion value:

49 pcs / [24] = [2.04 box]

Final answer:

3 pack and 13 pcs = 2.04 box

Major rules:

Here are some noticeable rules during the process:
1. When converting to smallest UOM, use multiplication.
2. Smallest UOM does not need to be converted.
3. When converting from smallest UOM to bigger UOM, use division.

Unit of Measure Conversion Pseudocode

Of course, all the examples above can be automated. The first is step is making a pseudo code.

Problem: 3 pack and 13 pcs is how many box?

// initialize converted value
converted_value = 0;

// set the product code
product_code = 'prod-1'

// set the target uom
target_uom = 'box';

// prepare objects (should be in loop and retrieved from database)
obj_1 = new obj();
obj_1.val = 3;
obj_1.uom = 'pack';

obj_1 = new obj();
obj_1.val = 3;
obj_1.uom = 'pack';

// put in array (if you're using PHP, you can use array_push while looping values from database)
input_obj_arr = {
	obj_1,
	obj_2
}

// variable for accumulated smallest uom
accumulated_smallest_uom_val = 0;

// loop through the array
foreach(input_obj_arr as input){

	// get the conversion value
	uom_conversion = getConversionValue(product_code, input.uom);

	// convert to smallest uom
	if(uom_conversion==null){
		// if uom_conversion is null, it means it is already the smallest uom
		// uom_conversion will be null since getConversionValue() method DO NOT get the smallest uom conversion value
	}

	else{
		// use multiplication to convert to smallest uom
		smallest_uom_val = input.val * uom_conversion.va;

		// accumulate smallest_uom
		accumulated_smallest_uom_val += smallest_uom_val;
	}
}

// now convert to the target uom
// getConversionValue() method will return null if it tries to get the smallest uom
target_uom_conversion = getConversionValue(product_code, target_uom);

// if target_uom_conversion is null, the accumulated_smallest_uom_val is as is the converted_value
if(target_uom_conversion==null){
	converted_value = accumulated_smallest_uom_val;
}

// compute the converted value by division
else{
	converted_value = accumulated_smallest_uom_val / target_uom_conversion.val;
}

3 pack and 13 pcs is 2.04 box (See example 5.9 above)

If you have any other way of solving this unit of measure conversion problem, please tell us in the comment section below. I am willing to change my current solution if yours is better.

Thanks for reading this post about unit of measure conversion pseudocode. If it helps you, please like or share it. Or if you have a friend or colleague that needs this type of solution, please share this to them, you’ll be happy you did. It’s always nice to help others.

Published
Categorized as Lists+

By Mike Dalisay

I'm Mike Dalisay, a pro web developer since 2010. I love web development. Improving our tutorials and source codes makes me happy. Do you want to suggest an edit to our tutorial? Got something to ask about our source codes? You may use our comments section below or email our team at [email protected]

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top