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 /
avatar image
0
Question by ina · Feb 24, 2012 at 12:00 PM · floatmonodecimal

Precision decimal data type

There's been a couple of links pointing to how Unity supports all the .NET / Mono data types, but it seems that precision data types, such as the double and decimal data types are implicitly converted to float.

I found this out the hard way after spending hours debugging something that turned out to be a decimal point precision issue.

As a sanity check, I tried:

 var p:System.Decimal = 12.3456789;
 Debug.Log(p.ToString());
 Debug.Log(p);

Believe it or not, both will output: 12.34568 , lopping off the digits after rounding
and NOT 12.3456789

I guess my question is, how do you properly deal with decimal point precision more than the float type, in Unity?

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

2 Replies

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

Answer by Jessy · Feb 24, 2012 at 02:38 PM

You need a suffix. Upper or lowercase should be fine, but UnityScript apparently requires the D to be capitalized, and won't accept either form of M. No idea why.

 var p:System.Decimal = 12.3456789; // float crammed into a decimal
 var p = 12.3456789m; // decimal
 var p = 12.3456789D; // double

Stop using UnityScript. The C# compiler would have told you what the problem was, and you wouldn't have had to wait for an answer.

Comment
Add comment · Show 4 · 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 ina · Feb 25, 2012 at 03:15 PM 0
Share

Why couldn't they wrap UnityScript better so the compiler returns the same errors as it would with C#

avatar image Jessy · Feb 25, 2012 at 03:33 PM 1
Share

I'm sure they could. But it takes man-hours. If you care, you need to at least log a bug about it. Personally, I don't care, because UnityScript always lags behind the progress of C#, and the languages are really similar aside from that, so I see no need for UnityScript to exist, and hope they discontinue it ASAP. :-D

avatar image ina · Feb 27, 2012 at 12:08 PM 0
Share

You forget that pretty much ALL the official Unity tutorials are in JS...

avatar image Jessy · Feb 27, 2012 at 03:30 PM 0
Share

I don't see why that matters. You should still know how to read UnityScript even if you don't use it. It looks almost exactly the same as simple C#.

avatar image
-2

Answer by jakovd · Feb 24, 2012 at 02:26 PM

I don't think this is an example representing decimal point precision problem. I think this is just string truncation acting as expected. If your number has too many characters to be printed out, its shown shortened. Putting a variable into Log() method implies that it should be converted to string and that is why you'll get the same result with .ToString() as you do without it.

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

7 People are following this question.

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

Related Questions

Unity/C# ignores decimal after it reach some value 1 Answer

How do I get rid of the numbers after the decimal? 2 Answers

On the accuracy of floats and doubles: Where/when do rounding errors usually occur, and how can I avoid them? 1 Answer

Does parseFloat depend on the OS? 2 Answers

Playerpref.SetInt enterd with a float. 2 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