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".
Do you mean hours, $$anonymous$$utes, seconds? Your question is not clear & it seems like a trivial question.
Still not clear. Your race time is a single float which get incremented, I get that. But what do you then want to do with it? Is this for display, storage, equations?
Answer by tanoshimi · Jun 16, 2015 at 11:47 AM
long longRaceTime = Convert.ToInt64(raceTime);
https://msdn.microsoft.com/en-us/library/y9www74y%28v=vs.110%29.aspx
By the way, is there a way to get the long to three decimals?
I tried
long longRaceTime = Convert.ToInt64(raceTime * 1000) / 1000;
But that didn't work.
You don't convert it to X decimal places, what you want is to display it in X decimal places. That's string.format
What would it look like in code, when adding it to
long longRaceTime = Convert.ToInt64(raceTime);?
Answer by UDN_077a3309-c352-4d18-bd5c-0ba0c6873aef · Nov 27, 2016 at 08:28 AM
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
Answer by Mark Gossage · Jun 18, 2015 at 07:03 AM
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"
Will this be okay when the plugin wants to send the score to the leaderboard in long format?
You're getting very confused between how the number is stored and how it's displayed. And I can't think of any game that would require a highscore to be saved as a long
- what plugin are you using?!
IOS Native: https://www.assetstore.unity3d.com/en/#!/content/7421
GameCenter$$anonymous$$anager.ReportScore (longRaceTime, leaderboardId);
Answer by nvnjls · May 05, 2017 at 07:12 AM
SECOND_MILLISECONDS = 1000;
public long SecFloatToLong(float seconds) { return (long)seconds * SECOND_MILLISECONDS; }
This should work
Your answer
Follow this Question
Related Questions
Cannot convert float to int? 1 Answer
Convert "Double" to "Float" or "Int"? 1 Answer
Double vs Float 1 Answer
Create a float with only 1 decimal? C# 1 Answer
[SOLVED!] PlayerPrefabs SetFloat and GetFloat is not working 0 Answers