Sunday, June 5, 2011

Converting Real Number to TIme

public static str 5 num2Time(real _hours)
{

    int realPart;
    int decimalPart;
    real hours;
    str hoursStr;
    str minStr;
    int decimalLocation;
    str finalMinutes;
    ;
    try
    {
        hours = _hours;
        hoursStr = num2str(hours, 0, 2, 1, 0);
        decimalLocation = strfind(hoursStr, ".", 0, strlen(hoursStr));
        hoursStr = substr(hoursStr, 0, decimalLocation);
        hoursStr = trimString(hoursStr, ["."]);
        minStr = num2str(hours, 0, 2, 1, 0);
        minStr = substr(minStr, decimalLocation, strlen(minStr) - strlen(hoursStr));
        minStr = trimString(minStr, ["."]);
        realPart = str2int(hoursStr);
        decimalPart = str2int(minStr);
        finalMinutes = hoursStr + ":" + (int2str(60 * decimalPart / 100) == "0" ? "00" : int2str(60 * decimalPart / 100));
        return finalMinutes;
    }
    catch
    {
        return num2str(_hours, 0, 2, 1, 0);
    }
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I opted for a single line conversion:
    strFmt("%1:%2", hours div 1, strRfix(int2str(round(abs(hours)*60,1) mod 60),2,"0"))

    ReplyDelete