Skip to main content
edited tags; edited tags; edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
deleted 65 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

up vote 0 down vote favorite II have a time, in seconds, which has the possibility to be very large. I wish to convert said time into the "appropriate" rounded, readable format.

I already have code which achieves this, however it's not very efficient (and contains a whole bunch of magic numbers):

String readable = decayTime + " minutes";
if(decayTime > 60)
{
    decayTime /= 60;
    readable = decayTime + " hours";

    if(decayTime > 24)
    {
        decayTime /= 24;
        readable = decayTime + " days";

        if(decayTime > 365)
        {
            decayTime /= 365;
            readable = decayTime + " years";

            if(decayTime > 1000000)
            {
                decayTime /= 1000000;
                readable = decayTime + "mn years";

                if(decayTime > 1000)
                {
                    decayTime /= 1000;
                    readable = decayTime + "bn years"; 
                }
            }
        }
    }

Apart from switching out the magic numbers, I can't personally think how to make it better.

What I am asking is essentially what would be a better approach to this, or is there something in-built which could help?

up vote 0 down vote favorite I have a time, in seconds, which has the possibility to be very large. I wish to convert said time into the "appropriate" rounded, readable format.

I already have code which achieves this, however it's not very efficient (and contains a whole bunch of magic numbers):

String readable = decayTime + " minutes";
if(decayTime > 60)
{
    decayTime /= 60;
    readable = decayTime + " hours";

    if(decayTime > 24)
    {
        decayTime /= 24;
        readable = decayTime + " days";

        if(decayTime > 365)
        {
            decayTime /= 365;
            readable = decayTime + " years";

            if(decayTime > 1000000)
            {
                decayTime /= 1000000;
                readable = decayTime + "mn years";

                if(decayTime > 1000)
                {
                    decayTime /= 1000;
                    readable = decayTime + "bn years"; 
                }
            }
        }
    }

Apart from switching out the magic numbers, I can't personally think how to make it better.

What I am asking is essentially what would be a better approach to this, or is there something in-built which could help?

I have a time, in seconds, which has the possibility to be very large. I wish to convert said time into the "appropriate" rounded, readable format.

I already have code which achieves this, however it's not very efficient (and contains a whole bunch of magic numbers):

String readable = decayTime + " minutes";
if(decayTime > 60)
{
    decayTime /= 60;
    readable = decayTime + " hours";

    if(decayTime > 24)
    {
        decayTime /= 24;
        readable = decayTime + " days";

        if(decayTime > 365)
        {
            decayTime /= 365;
            readable = decayTime + " years";

            if(decayTime > 1000000)
            {
                decayTime /= 1000000;
                readable = decayTime + "mn years";

                if(decayTime > 1000)
                {
                    decayTime /= 1000;
                    readable = decayTime + "bn years"; 
                }
            }
        }
    }

Apart from switching out the magic numbers, I can't personally think how to make it better.

What would be a better approach to this, or is there something in-built which could help?

Source Link

Convert length of time to appropriate unit

up vote 0 down vote favorite I have a time, in seconds, which has the possibility to be very large. I wish to convert said time into the "appropriate" rounded, readable format.

I already have code which achieves this, however it's not very efficient (and contains a whole bunch of magic numbers):

String readable = decayTime + " minutes";
if(decayTime > 60)
{
    decayTime /= 60;
    readable = decayTime + " hours";

    if(decayTime > 24)
    {
        decayTime /= 24;
        readable = decayTime + " days";

        if(decayTime > 365)
        {
            decayTime /= 365;
            readable = decayTime + " years";

            if(decayTime > 1000000)
            {
                decayTime /= 1000000;
                readable = decayTime + "mn years";

                if(decayTime > 1000)
                {
                    decayTime /= 1000;
                    readable = decayTime + "bn years"; 
                }
            }
        }
    }

Apart from switching out the magic numbers, I can't personally think how to make it better.

What I am asking is essentially what would be a better approach to this, or is there something in-built which could help?