1

I have a float that contains the total seconds the application is active.

For example float totalTime=523; means the application is active for 523 seconds

Now I want to change that value to a float that will represent minutes and seconds.

If i do

float timeResult=523/60;

i will get timeResult as 8.71

while minutes are correct, the seconds are not as they are scaled between 0 to 100 while it needs to be from 0 to 60

I need this to save this value as float since using application analytics with a float is easier to sort.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Dror
  • 1,262
  • 3
  • 21
  • 34
  • 1
    You can't scale a float between 0 and 60. It's built on the decimal system. Maybe a TimeSpan would be more use to you – ADyson Aug 20 '19 at 06:23
  • "it needs to be from 0 to 60". Nope! It doesn't need to. What are you trying to do? If you want to print this value, you ought to not use a point as separator, otherwise, it's not indicative of the unit you're using. You most certainly want to print a colon as separator. I don't recommend you cram both minutes and seconds into the same number, which may even print with more than two decimal digits. Use two separate integral numbers. – Johan Boulé Aug 20 '19 at 06:24
  • 1
    `523%60 = 43` `523/60 = 8.71` possible use '%' and '/' and combine it ?? – TimChang Aug 20 '19 at 06:24
  • 2
    Could you please explain why you want a float that represents the time as .? As @ADyson pointed out, this is not the normal way a float works. – Christiaan Nieuwlaat Aug 20 '19 at 06:25
  • @ChristiaanNieuwlaat Because I need to save that value in an analytics application. Working with float is more efficient as it allows me to sort and do averages. I could have used TimeSpan and save values as a string, but that removes the sorting/math purposes. – Dror Aug 20 '19 at 06:27
  • What kind of result do you want to achieve? 8.43 as a single float number? Two numbers for minutes and seconds? – default locale Aug 20 '19 at 06:28
  • "analytics with a float is easier to sort." I dont understand It How easier to sort? – TimChang Aug 20 '19 at 06:28
  • I mean, you can do math with numerical values, while with strings it becomes more complicated with an external program that saves analytics data (app start time, app start end, etc..) for your application – Dror Aug 20 '19 at 06:30
  • 2
    Why don't you keep it in seconds and at the output do some magic? I would never do this since it is not maintainable... – Mario The Spoon Aug 20 '19 at 06:30
  • @MarioTheSpoon Good question, since if some uses application for an hours or more, I will see huge numbers and the last thing i want is to start calculating times in my head, I have enough stuff in it already :) – Dror Aug 20 '19 at 06:31
  • 2
    But if you do maths with the float 8.43 as minutes, you'll get the wrong answer – iakobski Aug 20 '19 at 06:32
  • @Dror Please reconsider your approach. If you're going to do calculations (averages etc) with this "augmented" value, the result will definitely be wrong. In that case I'd personally stick with seconds instead – Christiaan Nieuwlaat Aug 20 '19 at 06:34
  • @ChristiaanNieuwlaat You have a point, didn't think it can make wrong averages trying to limit the float range :) I guess I'll calculate averages differently – Dror Aug 20 '19 at 06:39
  • Another solution to this is to keep the value as 523 in the background for sorting etc. And then just **display** it in minutes/seconds for the benefit of human readers. This is a pretty standard approach to dates and times – ADyson Aug 20 '19 at 06:43

6 Answers6

2

If you want say 61 seconds to equal 1.01. Then you need division and modulo, then simply divide the remainder by 100 then add the results together.

Note : This is fairly suspect and can cause all sorts of problems with calculations in the future

var minutes = val / 60;
var seconds = (val % 60) / 100m;
Console.WriteLine(minutes+seconds);
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

I'd recommend using appropriate datatype for that task instead of float:

var duration = TimeSpan.FromSeconds(573);

Then you can take advantage of such properties as TotalMinutes etc.

float is very bad choice for this, but since you need it, you can use created variable in a very clean way:

float f = (float) (duration.Minutes + duration.Seconds / 100f);
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
1
var totalTime = 523f;
var minutes = (int) (totalTime / 60);
var remainingSeconds = (int) (totalTime - minutes * 60);
System.Console.WriteLine(minutes.ToString() + ':' + remainingSeconds.ToString("00"));

Prints: 8:43

Johan Boulé
  • 1,936
  • 15
  • 19
0

Use the code here provided by Shipley:

using System;
TimeSpan time = TimeSpan.FromSeconds(seconds);
string str = time .ToString(@"hh\:mm\:ss\:fff");
Barış Akkurt
  • 2,255
  • 3
  • 22
  • 37
0

If you really need this to return a float on the given structure (which I personally think could be a recipe for trouble later on) you could do a more elaborate kind of trickery

float fMinutes = Math.Floor(<value> / 60.0F)
int iSeconds = <value> % 60

float constructedFloatValue = fMinutes + (iSeconds / 100.0F)

Please reconsider if you REALLY need this construct, before trying to use it.

0

You can use this logic if you seriously want to save it in float. This is not the recommended solution though.

            float totalTime = 523f;
            float remainingSeconds = (totalTime % 60f);
            int intResult = (int)(totalTime / 60);

            float finalResult = intResult + remainingSeconds / (float)(Math.Pow(10, remainingSeconds.ToString().Length));
Nikhil
  • 3,387
  • 1
  • 8
  • 16