Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 131913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:10:48+00:00 2026-05-11T06:10:48+00:00

I have a C# app and I need to convert between 3 different units

  • 0

I have a C# app and I need to convert between 3 different units (say for example: litres, gallons, and pints).

The app needs to know about certain volumes of liquid, say: 1 pint, 10 pints, 20 pints and 100 pints. I intend to do the calculations and hard code the values (not ideal but necessary),

I’m looking for a data structure that will allow me to easily convert from one unit to another.

Any suggestions?

Please note: I’m not actually using volumes of liquid, its just an example!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:10:49+00:00Added an answer on May 11, 2026 at 6:10 am

    You can store a matrix of conversion factors where

    • a: Is litres
    • b: Is pints
    • c: Are gallons

    You’d have (not accurate, but assuming there are two pints to a litre and 4 litres to a gallon)

       a     b       c a  1     2     0.25 b  0.5   1     0.125 c  4     8       1 

    Alternatively, you can decide that everything is converted to a base value (litres) before being converted to another type, then you just need the first line.

    Wrap this in a method that takes a number of units and ‘from’ type and ‘two’ type for the conversion.

    Hope this helps

    EDIT: some code, as requested

        public enum VolumeType     {         Litre = 0,         Pint = 1,         Gallon = 2     }      public static double ConvertUnits(int units, VolumeType from, VolumeType to)     {         double[][] factor =              {                 new double[] {1, 2, 0.25},                 new double[] {0.5, 1, 0.125},                 new double[] {4, 8, 1}             };         return units * factor[(int)from][(int)to];     }      public static void ShowConversion(int oldUnits, VolumeType from, VolumeType to)     {         double newUnits = ConvertUnits(oldUnits, from, to);         Console.WriteLine('{0} {1} = {2} {3}', oldUnits, from.ToString(), newUnits, to.ToString());     }       static void Main(string[] args)     {         ShowConversion(1, VolumeType.Litre, VolumeType.Litre);  // = 1         ShowConversion(1, VolumeType.Litre, VolumeType.Pint);   // = 2         ShowConversion(1, VolumeType.Litre, VolumeType.Gallon); // = 4         ShowConversion(1, VolumeType.Pint, VolumeType.Pint);    // = 1         ShowConversion(1, VolumeType.Pint, VolumeType.Litre);   // = 0.5         ShowConversion(1, VolumeType.Pint, VolumeType.Gallon);  // = 0.125         ShowConversion(1, VolumeType.Gallon, VolumeType.Gallon);// = 1         ShowConversion(1, VolumeType.Gallon, VolumeType.Pint);  // = 8         ShowConversion(1, VolumeType.Gallon, VolumeType.Litre); // = 4         ShowConversion(10, VolumeType.Litre, VolumeType.Pint);  // = 20         ShowConversion(20, VolumeType.Gallon, VolumeType.Pint); // = 160     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.