Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to convert pounds(Lb) to ounce(Oz) and Ounce(Oz) to Pounds(Lb) in C#.Net code

How to convert pounds(Lb) to ounce(Oz) and Ounce(Oz) to Pounds(Lb) in C#.Net code

Scheduled Pinned Locked Moved C#
csharpcollaborationtutorial
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    somagunasekaran
    wrote on last edited by
    #1

    Hi Team, I am working on unit of measure.. Just I want to know that logic part of the C#.Net source code for convert to Lb to Oz and Oz to Lb in drop down. If you are selecting as LB in one of the drop down and one more drop down selecting as a Oz and total converting calculation will be displayed in one of the text box. Please let me know how to convert this logic part. I am struggling now this logic part in drop down. Thanks Somasundaram

    I want to join in the website

    Richard DeemingR OriginalGriffO L 3 Replies Last reply
    0
    • S somagunasekaran

      Hi Team, I am working on unit of measure.. Just I want to know that logic part of the C#.Net source code for convert to Lb to Oz and Oz to Lb in drop down. If you are selecting as LB in one of the drop down and one more drop down selecting as a Oz and total converting calculation will be displayed in one of the text box. Please let me know how to convert this logic part. I am struggling now this logic part in drop down. Thanks Somasundaram

      I want to join in the website

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      1lb = 16oz If you're converting from lb to oz, multiply by 16. If you're converting from oz to lb, divide by 16. (Assuming you're not using Troy pounds/ounces[^].)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • S somagunasekaran

        Hi Team, I am working on unit of measure.. Just I want to know that logic part of the C#.Net source code for convert to Lb to Oz and Oz to Lb in drop down. If you are selecting as LB in one of the drop down and one more drop down selecting as a Oz and total converting calculation will be displayed in one of the text box. Please let me know how to convert this logic part. I am struggling now this logic part in drop down. Thanks Somasundaram

        I want to join in the website

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        There's no real "logic part" in the operation: it's all about identifying what the conversion factor is. I'm just guessing that you're a beginner and this is homework - so I won't explain how I'd do it (it probably involves some bits you haven't got to yet) and suggest that you pick a "base unit" and provide each possible "convertable unit" with a value which converts to that base unit. For example, if you select the kilogram as your base unit, then the conversion factor for 1lb would be 2.20462, and the conversion factor for 1oz would be 35.274 So to convert from "unit a" to "unit b" you multiply the number of items by the "unit b" conversion factor, divide that by the "unit a" convertion factor, and round it to perhaps 2 decimal places. If the number of lb is 6.0, then you would convert to oz by:

        6.0 * 35.274 / 2.20462 = 96.0002177246

        Rounded, that's 96.00 oz. So all you need to do is write a method which takes the selected unit text, and returns the conversion factor. Then call that twice, and you're pretty much there! Try it: it's simpler than it sounds!

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          There's no real "logic part" in the operation: it's all about identifying what the conversion factor is. I'm just guessing that you're a beginner and this is homework - so I won't explain how I'd do it (it probably involves some bits you haven't got to yet) and suggest that you pick a "base unit" and provide each possible "convertable unit" with a value which converts to that base unit. For example, if you select the kilogram as your base unit, then the conversion factor for 1lb would be 2.20462, and the conversion factor for 1oz would be 35.274 So to convert from "unit a" to "unit b" you multiply the number of items by the "unit b" conversion factor, divide that by the "unit a" convertion factor, and round it to perhaps 2 decimal places. If the number of lb is 6.0, then you would convert to oz by:

          6.0 * 35.274 / 2.20462 = 96.0002177246

          Rounded, that's 96.00 oz. So all you need to do is write a method which takes the selected unit text, and returns the conversion factor. Then call that twice, and you're pretty much there! Try it: it's simpler than it sounds!

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          OriginalGriff wrote:

          I'm just guessing that you're a beginner

          See description at somagunasekaran - Professional Profile - CodeProject[^].

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            OriginalGriff wrote:

            I'm just guessing that you're a beginner

            See description at somagunasekaran - Professional Profile - CodeProject[^].

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            :omg: THREE YEARS! And he can't do that on his own? Elephant! :wtf:

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            P M 2 Replies Last reply
            0
            • S somagunasekaran

              Hi Team, I am working on unit of measure.. Just I want to know that logic part of the C#.Net source code for convert to Lb to Oz and Oz to Lb in drop down. If you are selecting as LB in one of the drop down and one more drop down selecting as a Oz and total converting calculation will be displayed in one of the text box. Please let me know how to convert this logic part. I am struggling now this logic part in drop down. Thanks Somasundaram

              I want to join in the website

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you are really "working on unit of measure", you should take a moment to consider the longer term; e.g. a "weight structure / class". The weight could have a "native" value, but would have methods for returning different UOMs: all ounces or grams; or pound / kilogram fractions; etc.

              public class MyWeight {

              public decimal Weight { get; set; }
              public UnitOfMeasure Units { get; set; }

              public MyWeight( decimal weight, UnitOfMeasure units = UnitOfMeasure.Pounds ) {
              this.Weight = weight;
              this.Units = units;
              }

              public decimal InPounds( int decimalPlaces = 0 ) {

                decimal qty = 0M;
              
                switch ( this.Units ) {
                   case UnitOfMeasure.Pounds:
                      qty = Weight;
                      break;
                   case UnitOfMeasure.Ounces:
                      qty = Weight \* 0.0625M;
                      break;
                   case UnitOfMeasure.Kilograms:
                      qty = Weight \* 2.20462M;
                      break;
                   case UnitOfMeasure.Grams:
                      qty = Weight \* 0.00220462M;
                      break;
                   default:
                      throw new NotImplementedException( string.Format( "{0} to Pounds.", Units ) );
                }  // end switch:
              
                qty = RoundUp( qty, decimalPlaces );
              
                return qty;
              

              }

              // Etc.

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                :omg: THREE YEARS! And he can't do that on his own? Elephant! :wtf:

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Be fair, he said it was three years in the field. Very possibly there was no computer in this field. Perhaps there were some sheep.

                This space for rent

                OriginalGriffO 1 Reply Last reply
                0
                • P Pete OHanlon

                  Be fair, he said it was three years in the field. Very possibly there was no computer in this field. Perhaps there were some sheep.

                  This space for rent

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Even the sheep could have worked it out by now! :laugh:

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    :omg: THREE YEARS! And he can't do that on his own? Elephant! :wtf:

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    And the profile is 9 years old so it could mean that he has been around for 12 years :laugh:

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups