Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Mon Jun 05 2023

Convert shoe sizes from millimeter/inches to EU/US/UK sizes and vice versa

In this tutorial, we will do some shoe size conversion. We will create functions for converting from millimeter or inches to the different sizes, and also converting between the different size units.

Formulas

We will start with some formulas that we will go through before we start writing our functions.

Millimeter to inch

inch = millimeters / 25.4

Inch to millimeter

millimeter = inch x 25.4

US sizes

male = (3 × inches) - 22 female = (3 × inches) - 21 child = (3 × inches) - 9.67

UK sizes

adult = (3 × inches) - 23 child = (3 × inches) - 10

EU

adult = 1.27 × (3 * inches) + 2

Code

Now when we have all our formulas written down, we will start with the coding part.

Converting millimeter to inch

Let's create a function for converting millimeters to inches, since our formulas are using inches as base.

const toInches = (millimeters) => { return millimeters / 25.4; };

Round

We want our shoe sizes to get rounded to the nearest .5, so let's create a util function for that as well

const round = (number) => Math.round(number * 2) / 2;

Convert inches to US

Next, we will create a function for converting inches to the different US sizes (male, female and child)

const toUS = (inches) => { return { male: round(3 * inches - 22), female: round(3 * inches - 21), child: round(3 * inches - 9.67), }; };

Convert inches to UK

For UK conversion, we do similar as for US

const toUK = (inches) => { return { adult: round(3 * inches - 23), child: round(3 * inches - 10), }; };

Convert inches to EU

And lastly, we convert our EU size as well

const toEU = (inches) => { return { size: round(1.27 * (3 * inches) + 2), }; };

Convert foot length to size

Now we will create a new function for converting the length of the foot to a shoe size. The function should take the foot length unit into consideration as well.

We start with declaring our function as below, where we expect it to get the length, unit of the length and also which shoe size unit to use (EU/UK/US). We are predefining the unit to inch and the sizeUnit to US.

const lengthToSize = (length, unit = "inch", sizeUnit = "US") => {};

Then, we will take care of our foot length problem. If unit is set to millimeter, we need to convert it to inches, if unit is set to inch, we do nothing.

const inches = unit === "millimeter" ? toInches(length) : length;

Next, we will convert to the correct sizeUnit and return it. The function should now look like this:

const lengthToSize = (length, unit = "inch", sizeUnit = "US") => { const inches = unit === "millimeter" ? toInches(length) : length; if (sizeUnit === "US") { return toUS(inches); } else if (sizeUnit === "EU") { return toEU(inches); } else if (sizeUnit === "UK") { return toUK(inches); } throw new Error(`${sizeUnit} not supported. Supported units are: UK/US/EU`); };

Test our lengthToSize() function

Before we proceed, we will test our function with some test data.

console.log(lengthToSize(9)); // { male: 5, female: 6, child: 17.5 } console.log(lengthToSize(9, "inch", "UK")); // { adult: 4, child: 17 } console.log(lengthToSize(300, "millimeter")); // { male: 13.5, female: 14.5, child: 26 } console.log(lengthToSize(320, "millimeter", "EU")); // { size: 50 } lengthToSize(4, "inch", "KU"); // Error: KU not supported. Supported units are: UK/US/EU

There we go. Now our function is able to give us the correct shoe sizes from a given length of the foot, from both millimeter and inches.

Convert between the shoe sizes

In order to convert between the different shoe sizes, like UK to US, we need to first convert the size to inches, and then convert inches to the other two shoe sizes.

We will create some functions, that will do the opposite to our inches -> shoe size functions.

const USToInch = (size) => { return { male: (size + 22) / 3, female: (size + 21) / 3, child: (size + 9.67) / 3, }; }; const UKToInch = (size) => { return { adult: (size + 23) / 3, child: (size + 10) / 3, }; }; const EUtoInch = (size) => { const result = (size - 2) / 1.27; return result / 3; };

Now we can get inches from our shoe sizes.

Next, we will do our conversion function, where we specify the unit, size and also the "type" of our size. US has male, female and child whilst UK has adult and child and EU has none. We will then convert from the size that we have, to the other two.

const convertShoeSizes = (from, size, type) => { if (from === "EU") { } else if (from === "UK") { if (type !== "adult" && type !== "child") { throw new Error("UK must have type specified to either adult or child"); } } else if (from === "US") { if (type !== "male" && type !== "female" && type !== "child") { throw new Error( "US must have type specified to either male, female or child" ); } } throw new Error(`${from} not supported. Supported units are: UK/US/EU`); };

All right, so now we have our checks setup. So if we have a EU size, we should get the inches and then convert to UK and US, and then the same for the others.

So for EU, we will do like this:

if (from === "EU") { const inches = EUtoInch(size); return { UK: toUK(inches), US: toUS(inches), }; }

And for UK:

else if (from === "UK") { if (type !== "adult" && type !== "child") { throw new Error("UK must have type specified to either adult or child"); } const inches = UKToInch(size)[type]; return { EU: toEU(inches), US: toUS(inches), }; }

And for US:

else if (from === "US") { if (type !== "male" && type !== "female" && type !== "child") { throw new Error( "US must have type specified to either male, female or child" ); } const inches = USToInch(size)[type]; return { EU: toEU(inches), UK: toUK(inches), }; }

Full code of convertShoeSizes()

const convertShoeSizes = (from, size, type) => { if (from === "EU") { const inches = EUtoInch(size); return { UK: toUK(inches), US: toUS(inches), }; } else if (from === "UK") { if (type !== "adult" && type !== "child") { throw new Error("UK must have type specified to either adult or child"); } const inches = UKToInch(size)[type]; return { EU: toEU(inches), US: toUS(inches), }; } else if (from === "US") { if (type !== "male" && type !== "female" && type !== "child") { throw new Error( "US must have type specified to either male, female or child" ); } const inches = USToInch(size)[type]; return { EU: toEU(inches), UK: toUK(inches), }; } throw new Error(`${from} not supported. Supported units are: UK/US/EU`); };

Testing

Let's do some conversions between our different units and see if it works

console.log(convertShoeSizes("EU", 40)); /* { UK: { adult: 7, child: 20 }, US: { male: 8, female: 9, child: 20.5 } } */ console.log(convertShoeSizes("EU", 45)); /* { UK: { adult: 11, child: 24 }, US: { male: 12, female: 13, child: 24 } } */ console.log(convertShoeSizes("UK", 9.5, "adult")); // { EU: { size: 43.5 }, US: { male: 10.5, female: 11.5, child: 23 } } console.log(convertShoeSizes("UK", 14, "child")); // { EU: { size: 32.5 }, US: { male: 2, female: 3, child: 14.5 } } console.log(convertShoeSizes("US", 7, "female")); // { EU: { size: 37.5 }, UK: { adult: 5, child: 18 } } console.log(convertShoeSizes("US", 9, "male")); // { EU: { size: 41.5 }, UK: { adult: 8, child: 21 } }

And there we go, now we can convert between all the different shoe size units and types.

Full code of todays tutorial

const toInches = (millimeters) => { return millimeters / 25.4; }; const round = (number) => Math.round(number * 2) / 2; const toUS = (inches) => { return { male: round(3 * inches - 22), female: round(3 * inches - 21), child: round(3 * inches - 9.67), }; }; const USToInch = (size) => { return { male: (size + 22) / 3, female: (size + 21) / 3, child: (size + 9.67) / 3, }; }; const toUK = (inches) => { return { adult: round(3 * inches - 23), child: round(3 * inches - 10), }; }; const UKToInch = (size) => { return { adult: (size + 23) / 3, child: (size + 10) / 3, }; }; const toEU = (inches) => { return { size: round(1.27 * (3 * inches) + 2), }; }; const EUtoInch = (size) => { const result = (size - 2) / 1.27; return result / 3; }; const lengthToSize = (length, unit = "inch", sizeUnit = "US") => { const inches = unit === "millimeter" ? toInches(length) : length; if (sizeUnit === "US") { return toUS(inches); } else if (sizeUnit === "EU") { return toEU(inches); } else if (sizeUnit === "UK") { return toUK(inches); } throw new Error(`${sizeUnit} not supported. Supported units are: UK/US/EU`); }; const convertShoeSizes = (from, size, type) => { if (from === "EU") { const inches = EUtoInch(size); return { UK: toUK(inches), US: toUS(inches), }; } else if (from === "UK") { if (type !== "adult" && type !== "child") { throw new Error("UK must have type specified to either adult or child"); } const inches = UKToInch(size)[type]; return { EU: toEU(inches), US: toUS(inches), }; } else if (from === "US") { if (type !== "male" && type !== "female" && type !== "child") { throw new Error( "US must have type specified to either male, female or child" ); } const inches = USToInch(size)[type]; return { EU: toEU(inches), UK: toUK(inches), }; } throw new Error(`${from} not supported. Supported units are: UK/US/EU`); };

Summary

So this article was a little bit odd perhaps, I don't think this is a very common scenario to face, but still, if you are working on a web shop that are selling shoes or similar, it can perhaps become handy to do some conversions :)

Anyway, I hope you enjoyed this article and that it helped you!

signatureMon Jun 05 2023
See all our articles