Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by OwlTeaGames · Feb 21, 2020 at 03:09 AM · playerprefsfloatintconversion

Converting from an Int to a Float and back with PlayerPrefs

Hello, I'm having a weird issue. I've been looking around for a couple hours and I feel like I'm losing my mind.

I'm trying to create a PlayerPref based on two other Prefs, and it's just not working right.

 int kills = PlayerPrefs.GetInt("CurrentKills");
 int shotsFired = PlayerPrefs.GetInt("ShotsFired");
 
 float fPrecision = kills / shotsFired;
 
 int iPrecision = (int)fPrecision;
 
 PlayerPrefs.SetInt("Precision", iPrecision);

I saw a post that was using ToString, but I have no idea how to make it work!

 floatValue.ToString("0");
 

I don't get it. Am I supposed to put something where the zero is? Or what? I just see a lot of people saying, "Hey, it worked for me!" but nobody actually shows how to implement it. Thanks in advance!

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Feb 21, 2020 at 03:18 AM

I guess that "kills" will always be less or equal to "shotsFired". This expression:

 kills / shotsFired;

will not produce a float value. Since both operands are integer values this will perform an integer division and the result is also an integer. As soon as shotsFired is greater than kills this expression will always be 0. Only when both values are the same you would get a value of 1.


In order to perform a floating point division at least one of the operands need to be a float value.

 float fPrecision = (float)kills / shotsFired;

Of course if your precission is less than 1.0 casting the float value back into an integer will of course again result in a value of 0. For example if you have 30 kills and 48 shots fired you get a float value of "0.625" (so about 62.5 percent). However casting 0.625 into an integer will floor the value back to "0". Maybe you wanted the value in actual percent? If that's the case you have to multiply your ratio by 100.

Comment
Add comment · Show 1 · 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 OwlTeaGames · Feb 21, 2020 at 03:36 AM 0
Share

Thank You! This was exactly the answer I needed! All I had was 11 points, but I sent them over as a thank you.

Here's the final code for anybody who may need it:

         int kills = PlayerPrefs.GetInt("Current$$anonymous$$ills");
         int shotsfired = PlayerPrefs.GetInt("ShotsFired");
 
         float fPrecision = ((float)kills / shotsfired) * 100; //percentage!
 
         int iPrecision = (int)fPrecision;
 
         PlayerPrefs.SetInt("Precision", iPrecision);
 

It worked perfectly!

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

127 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot implicitly convert type `float' to `int 1 Answer

C# Int to Float conversion performance 3 Answers

How to use PlayerPrefs 4 Answers

problem with playerprefs 1 Answer

How would I transfer these into PlayerPrefs? 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