Can "return" be applied to a String?

#1

I wrote this little function to convert miliseconds to Human readible time.

I would like to use “return” to make life simple.

So I tried:

 void MsConversion(int MS)

{
int totalSec= (MS / 1000);
int seconds = (MS / 1000) % 60;
int minutes = (MS / (1000*60)) % 60;
int hours = ((MS/(1000*60*60)) % 24);                      
 HumanTime= (hours+":" +nf(minutes, 2, 0)+ ":"+ nf(seconds, 2, 0));
return HumanTime;

//println (HumanTime);
}

but I get:

Void methods cannot return a value

Finally, I would want to use this function like this:

String ReadHere = MsConversion (8998798798);

and obtain a nice 8:44:43 for the string.

Is it possible?
Thanks

1 Like
#2

If you want to return a value from a function, you have to give your function a return type. In your case it would probably look like this:

String MsConversion(int MS) {
  // ...
3 Likes
#3

you just need to set the return type of the method

void returns nothing but you can replace it with just about any other type so replace void with String and your method will work

String MsConversion(int MS)
{
   int totalSec= (MS / 1000);
   int seconds = (MS / 1000) % 60;
   int minutes = (MS / (1000*60)) % 60;
   int hours = ((MS/(1000*60*60)) % 24);                      
   
   HumanTime= (hours+":" +nf(minutes, 2, 0)+ ":"+ nf(seconds, 2, 0));
   
   return HumanTime;
   //println (HumanTime);
}
1 Like
#4

Thanks , that was easy

Mitch