Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
-1
Question by Abacab · Jun 16, 2015 at 06:14 AM · c#floatconvertlong

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".

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mark Gossage · Jun 16, 2015 at 06:16 AM 1
Share

Do you mean hours, $$anonymous$$utes, seconds? Your question is not clear & it seems like a trivial question.

avatar image Abacab · Jun 16, 2015 at 06:18 AM 0
Share
 raceTime = raceTime + 1 * Time.deltaTime;
avatar image Mark Gossage · Jun 16, 2015 at 08:36 AM 0
Share

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?

avatar image Abacab · Jun 16, 2015 at 09:02 AM 0
Share

A leaderboard plugin sends the score as long.

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Abacab · Jun 16, 2015 at 08:01 PM 0
Share

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.

avatar image Mark Gossage · Jun 17, 2015 at 02:49 PM 0
Share

You don't convert it to X decimal places, what you want is to display it in X decimal places. That's string.format

avatar image Abacab · Jun 18, 2015 at 06:47 AM 0
Share

What would it look like in code, when adding it to

long longRaceTime = Convert.ToInt64(raceTime);?

avatar image
3

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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"
  
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Abacab · Jun 21, 2015 at 06:38 PM 0
Share

Will this be okay when the plugin wants to send the score to the leaderboard in long format?

avatar image tanoshimi · Jun 21, 2015 at 08:51 PM 0
Share

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?!

avatar image Abacab · Jun 22, 2015 at 09:42 AM 0
Share

IOS Native: https://www.assetstore.unity3d.com/en/#!/content/7421

 GameCenter$$anonymous$$anager.ReportScore (longRaceTime, leaderboardId);
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges