How to convert float to long

Hello!

I need to convert my float raceTime (three decimals) to long raceTime. How do I do that? I need to send the score to a Game Center leaderboard that is set to “Fixed Point - To 3 Decimals”.

long longRaceTime = Convert.ToInt64(raceTime);

I managed to combine some info from other answers with a bit of trial and tribulation to come up with a way to convert a float with decimals to long for submitting to Services Leaderboards.

First you format the float to a string with 4dp, then you remove the “.” in the string and finally parse the string to a long.

  string s = string.Format("{0:0.0000}", 100.1234f);
  Social.ReportScore(long.Parse(s.Replace(".", "")),leaderboard, callback);

Cheers

What you are really looking for it displaying a float as an integer or a certain number of decimal places. This is using the string.format function
Eg.

float timer=123.456789;

string s=string.format("{0:0}",timer); // "123"
string s=string.format("{0:0.0}",timer); // "123.5"
string s=string.format("{0:0.000}",timer); // "123.457"
string s=string.format("{0:0}m {1:0.00}s",int(timer/60), timer%60 ); // "2m 3.46s"

SECOND_MILLISECONDS = 1000;

public long SecFloatToLong(float seconds)
{
return (long)seconds * SECOND_MILLISECONDS;
}

This should work