Convert decimal representing time to hour, minutes, seconds

前端 未结 4 1037
一向
一向 2020-12-21 02:45

I have a decimal. The range of this decimal is between 0 and 23.999999. This decimal represents a time. For example, if the decimal is 0.25, then the time it represents is 1

4条回答
  •  时光取名叫无心
    2020-12-21 02:50

    Whatever language you use, you can do this using the math functions: MOD and FLOOR/TRUNC

    Let "dec" be the decimal variable

    trunc(mod(dec, 1)) => hours
    trunc(mod(dec * 60, 60)) => minutes
    trunc(mod(dec * 3600, 60)) => seconds
    

    In C#, you can truncate a decimal to int using just explicit casting, e.g.

    int seconds = (int) ((dec * 3600) % 60)
    

提交回复
热议问题