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. Converting Months into years.

Converting Months into years.

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 3 Posters 2 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
    Skanless
    wrote on last edited by
    #1

    I am trying to display then data that is captured in months to be shown as years. For example if someone have been with us for 18 months, I want to display it as 1.5 years. I have attempted to do so by writing the code below but to no avail. Any assistance will be greatly appreciated. public static int YearsFromMonths(int months) { int evenMonths = (months /12); int remainingMonths =(months % 12); decimal fractionOfYear = Convert.ToDecimal(((remainingMonths) * .0833)/10); if((months % 12) == 0) { return evenMonths; } else { return fractionOfYear; } Thanks.

    Skan If you knew it would not compile why didn't you tell me?!?!?!

    C P S 3 Replies Last reply
    0
    • S Skanless

      I am trying to display then data that is captured in months to be shown as years. For example if someone have been with us for 18 months, I want to display it as 1.5 years. I have attempted to do so by writing the code below but to no avail. Any assistance will be greatly appreciated. public static int YearsFromMonths(int months) { int evenMonths = (months /12); int remainingMonths =(months % 12); decimal fractionOfYear = Convert.ToDecimal(((remainingMonths) * .0833)/10); if((months % 12) == 0) { return evenMonths; } else { return fractionOfYear; } Thanks.

      Skan If you knew it would not compile why didn't you tell me?!?!?!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If you want to return a fraction, return a float. float years = months/12.0F;

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      S 2 Replies Last reply
      0
      • C Christian Graus

        If you want to return a fraction, return a float. float years = months/12.0F;

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        S Offline
        S Offline
        Skanless
        wrote on last edited by
        #3

        This may sound ignorant but what's the difference between Decimal and float? The couple of Apps I wrote in C++ I never heard of decimal. I guess this new with .Net. Also why the "F" after the number for float? .Net suck...........but I guess I have to adapt.

        Skan If you knew it would not compile why didn't you tell me?!?!?!

        1 Reply Last reply
        0
        • S Skanless

          I am trying to display then data that is captured in months to be shown as years. For example if someone have been with us for 18 months, I want to display it as 1.5 years. I have attempted to do so by writing the code below but to no avail. Any assistance will be greatly appreciated. public static int YearsFromMonths(int months) { int evenMonths = (months /12); int remainingMonths =(months % 12); decimal fractionOfYear = Convert.ToDecimal(((remainingMonths) * .0833)/10); if((months % 12) == 0) { return evenMonths; } else { return fractionOfYear; } Thanks.

          Skan If you knew it would not compile why didn't you tell me?!?!?!

          P Offline
          P Offline
          Pratik Vasant Shah
          wrote on last edited by
          #4

          Hi, If its 18 months you will show as 1.5 years. What if its 16 months or 15 months. How will that be displayed. This is for my information, so that I can work on it

          Thanking you in Advance Kind Regards Pratik Shah

          S 1 Reply Last reply
          0
          • P Pratik Vasant Shah

            Hi, If its 18 months you will show as 1.5 years. What if its 16 months or 15 months. How will that be displayed. This is for my information, so that I can work on it

            Thanking you in Advance Kind Regards Pratik Shah

            S Offline
            S Offline
            Skanless
            wrote on last edited by
            #5

            If its 15 months then it should display 1.25 and if its 16 months then should display 1.3 (not the on decimal place because in reality it 1.3333). I think if we take the radical vairable and multiply it by .83333 then round it off to on or two decimal place that should work.

            Skan If you knew it would not compile why didn't you tell me?!?!?!

            1 Reply Last reply
            0
            • C Christian Graus

              If you want to return a fraction, return a float. float years = months/12.0F;

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              S Offline
              S Offline
              Skanless
              wrote on last edited by
              #6

              Also is it possible to round a float number to one deciaml place?

              Skan If you knew it would not compile why didn't you tell me?!?!?!

              1 Reply Last reply
              0
              • S Skanless

                I am trying to display then data that is captured in months to be shown as years. For example if someone have been with us for 18 months, I want to display it as 1.5 years. I have attempted to do so by writing the code below but to no avail. Any assistance will be greatly appreciated. public static int YearsFromMonths(int months) { int evenMonths = (months /12); int remainingMonths =(months % 12); decimal fractionOfYear = Convert.ToDecimal(((remainingMonths) * .0833)/10); if((months % 12) == 0) { return evenMonths; } else { return fractionOfYear; } Thanks.

                Skan If you knew it would not compile why didn't you tell me?!?!?!

                S Offline
                S Offline
                Skanless
                wrote on last edited by
                #7

                This one line of code solved it all. Thanks guys. return Decimal.Round(months/12M,1);

                Skan If you knew it would not compile why didn't you tell me?!?!?!

                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