Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 koral293a · Aug 31, 2021 at 01:15 AM · bugfloatdecimal

Unity/C# ignores decimal after it reach some value

Hello, I got a little problem. I am creating a clicker game and when the money ammount reaches over 100 000, the 2nd decimal digit stops moving, then when it reaches 1 000 000 the 1st decimal digits stops moving also. My code looks like that:

 float currentMoney = 0.00f;
 
 private void Update()
     {
           currentMoney += 11111.11f;
     }

For example:

  • 1st frame - currentMoney = 99 999.55

  • 2nd frame - currentMoney = 100 000.50

  • 3rd frame - currentMoney = 100 111.60

  • X frame - currentMoney = 999 999 .30

  • X+1 frame - currentMoney = 1 000 011 .00

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
4

Answer by Eno-Khaon · Aug 31, 2021 at 04:17 AM

That's how Floating Point numbers work.

That said, the main subject of this question, Floating Point Accuracy, comes up fairly frequently. This isn't an uncommon subject, but the context in which it is brought up will vary. So, I'll keep it brief.


Unity uses 32-bit ( float) and 64-bit ( double) Floating Point values, with an emphasis on 32-bit.

Unlike integers, however, the data in Floating Point numbers needs to be divided up a bit.

A float is comprised of a 23-bit number, an 8-bit mantissa (exponent), and 1-bit for the positive/negative sign. Essentially, you can think of it as a 23-bit integer where you can place the decimal point wherever you want around it.
 //        1234567.0
 //         123456.7
 //          12345.67
 //           1234.567
 //              0.000001234567
 // 12345670000000.0
 // etc.

Because it only uses 23-bits for the main value, however, it doesn't have a *huge* amount of accuracy available to it. This is the problem you're running into. Basically, as you increase the exponent (power of 10) of your value, you lose a digit of accuracy off the other end.


Having said all this, I'll also direct you to a pair of posts I previously made related to the subject of games using huge number libraries (or similar). Hopefully, those might help point you in your preferred direction.

Edit: Formatting
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

145 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 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

Rotation Values Garbled When Set By Code 2 Answers

Unity changing decimal points to decimal commas? 1 Answer

I dont want Decimals :P 1 Answer

How to fix wobbling float when away from spawn? 1 Answer

Javascript rounding off a decimal 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