Home > Back-end >  Time scaled from 0 to 1 in HH:MM
Time scaled from 0 to 1 in HH:MM

Time:11-07

I have a simple question (I am bad at math...). So i made a day/night cycle in my game and to change the time of the day i have slider ranging from 0 to 1. Everything is working correctly, but i can't convert this time in minutes.

Here is a bit of the code I am using:

Where time is my slider (float) and timeRate (float) a time multiplier.

    private void Update(){
        time  = timeRate * Time.deltaTime;
        ConvertTime();
    }


    private void ConvertTime()
    {
        hours = 24 * time;
        minutes = ?;

        if(minutes >= 60)
            minutes = 0;

        displayRealTime = LeadingZero(hours.ToString("0"))   ":"   "00";
    }

The "LeadingZero" function just add a 0 if the number < 10.

The hours are good but i don't know how to get the minutes. Thanks for reading.

CodePudding user response:

well your hours already contain the minutes after the decimal.

E.g.

12.23 hours

=> minutes is

0.23 * 60

So all you need is Get only decimals of a floating point number and multiply it by 60 and then round to int again

var minutes = Mathf.RountToInt((hours % 1f) * 60f);

Fiddle

  • Related