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 Noob_Vulcan · Dec 30, 2014 at 05:23 AM · c#stringfloatinteger

Floating Point and ToString issue

Hello guys,

I'm facing problem in finding the length a number .

  float tempF = 123456789564353410.111213f;
         string tempS = tempF.ToString();
         Debug.Log(tempS.Length);

SO this basically prints 12 .

I want the length of the integer part only (no decimals). I cannot do this too

  float tempF = 123456789564353410.111213f;
       int tempI=(int)tempF;

since the number is exceeding the int range.

So How can i find the length of Integer part in Float.

  float tempF = 123456789564353410.111213f;

Output i want is : 18

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
0
Best Answer

Answer by Noob_Vulcan · Dec 30, 2014 at 07:04 AM

Ok i found it ...

  float tempF = 123456789564353410.111213f;
  Math.Floor(Math.Log10(tempF)) + 1 ;



this will find the length

  • Math.Floor(Math.Log10(Your_Number)) + 1 **

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 Landern · Dec 30, 2014 at 07:07 AM 0
Share

If the floating point and precision isn't important, why not just use a string. If your float value has significance, you will lose it from go with numbers of that length. If you store as a string, well you can just lop off from the period to the length -1 with out all the math. Just curious.

avatar image Noob_Vulcan · Dec 30, 2014 at 07:15 AM 0
Share

@Landem : I tried with string .

    float tempF = 123456789564353410.111213f;
          string tempS = tempF.ToString();
          Debug.Log(tempS.Length);


this will print 12. It will print right only if ur number has max 7 digits

avatar image Landern · Dec 30, 2014 at 07:22 AM 0
Share

No, i was asking from go why aren't you taking it in as a string if you're only(from what i can see/limited view based on questions) using it as a length qualifier. If you're doing any math on it, the precision is lost(if it mattered) and if it didn't, sweet, but it must since you want the length of the number. Again, just feeding my curiosity ;)

avatar image Noob_Vulcan · Dec 30, 2014 at 07:38 AM 1
Share

@Landem : hahaha... Actually m perfor$$anonymous$$g lots of calculations on the numbers(upto 38 digit numbers ).so I need float precision there.I needed length to show the digits in the form of million,trillion,octillion etc. rather than showing 10,000,000,000,000,000 . :D

so if i have 10,000,000.98383 i can show 10.000 million . Rest of the precision is taken care of at the backend .. :P

avatar image
3

Answer by Landern · Dec 30, 2014 at 05:40 AM

first, use a decimal, it can hold more characters with percision then a float, plus decimal has a truncate method that removes the decimal and everything to the right.

     decimal tempF = 123456789564353410.111213M;
     int tempFLength = decimal.Truncate(tempF).ToString().Length;

This outputs 18 for me:

 System.Console.WriteLine("tempF Value: " + tempF.ToString() + "\n" + "tempF Length " + tempFLength);
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 Noob_Vulcan · Dec 30, 2014 at 06:12 AM 0
Share

@Landem : Thanks bro ... this works fine ( even i may use Decimal ). But Decimal cannot hold more characters than Float (not taking about precision). Float stores upto 39 Digits whereas Decimal stores upto 29 digits.

So i was thinking is there a way to do the same for float(since i may need to store upto 39 digit).

avatar image HarshadK · Dec 30, 2014 at 06:27 AM 0
Share

Try this: $$anonymous$$ath.Truncate $$anonymous$$ethod (Double).

Yeah, I know it's for double but worth a shot.

avatar image Landern · Dec 30, 2014 at 06:44 AM 0
Share

with out using decimal you're going to lose information with float(single) and double(double precision float). You can see this when you use the ToString method and it returns scientific notation/exponential values. If you try and format it it(.ToString("F") or .ToString("F20") you will see the precision up to the the number 8 from the left... so... using a double would give you more, up to the 3 from the left, but then... waxy waxy goes the numbers. The storage of float(single) and doubles are to include the loss and expand on the exponent.

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

26 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

Related Questions

Can I create a list with an int/float and a string? C# 2 Answers

Hiding decimals without using Mathf.Round? 2 Answers

Array not displaying my level best time in my level select menu 0 Answers

How to Display only current instead of current/max on a GUI.Box 1 Answer

Why are floats not accessing my inputted values? 1 Answer


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