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
2
Question by SuIXo3 · Jul 27, 2012 at 09:14 AM · c#floatintconvertdouble

Convert "Double" to "Float" or "Int"?

I searched around but nothing useful came. Just some help about javascript (about parseFloat), but I'm using C# and it does not seem to work.

I have a private double which I want to divide by 360 and then use as angle. Problem is quaternions need floats, it doesn't work with double.

Any help on how to achieve this?

Thanks in advance.

Comment
Add comment · Show 3
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 Kryptos · Jul 27, 2012 at 09:56 AM 1
Share

Use a float ins$$anonymous$$d of a double then.

avatar image Bunny83 · Jul 27, 2012 at 10:21 AM 1
Share

Exactly. In C# you have to give number literals an "f" suffix so it's actually a float. In UnityScript it's reverse. Un US the default type is float, in C# it's double.

These linen of code (which are valid in both languages) produce different variables depending on the language:

 var Var1 = 1.0;
 var Var2 = 1.0f;
 var Var3 = 1.0d;

In C# Var1 and Var3 would be of type double. Var2 would be a float.

In UnityScript Var1 and Var2 will be a float, Var3 would be a double.

However, i prefer to specify the type of the variables:

 //C#
 double Var1 = 1.0;
 float  Var2 = 1.0f;
 double Var3 = 1.0d;

 //UnityScript
 var Var1 : float = 1.0;
 var Var2 : float = 1.0f;
 var Var3 : double = 1.0d;
avatar image Patton7 Bunny83 · Jan 21, 2019 at 08:19 PM 0
Share

Thanks so much!!! this really helped. i dont know why i didn't think of this before lol

1 Reply

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

Answer by fafase · Jul 27, 2012 at 09:16 AM

I do not use C# but you could try:

 float variable = (float)yourDoubleVariable;

Edit: Information for other reader:

UnityScript allows to cast without any typecasting like

 var aFloat:float;
 var aInt:int;
 
 aInt= aFloat;

will work and the conversion will simply truncate the result, not round so 1.9 becomes 1.

P.S:For those of you wondering how to use larger data than float in UnityScript you need to include the System.

 var aDouble : System.Double;
 var aBigInt : System.Int64;

Comment
Add comment · Show 2 · 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 SuIXo3 · Jul 27, 2012 at 09:40 AM 0
Share

That seems to have worked, Thanks!

avatar image Whiteleaf · Feb 08, 2016 at 04:48 PM 0
Share

Casting it worked like a charm, even in C#. Thanks.

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

12 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

Related Questions

Cannot convert float to int? 1 Answer

Int not getting rounded data 0 Answers

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

Is there any way to subtract int / float if another int / float subtracted? 1 Answer

Having trouble converting this int to string, can someone help? 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