Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
1
Question by Shiro_Rin · Mar 13, 2016 at 06:09 AM · floatinttostringrounding

ToString ("f0") is rounding my float

Say my number is at 1.6, well my tostring text on screen is showing 2. I don't want it to do that, I only want it to show 1, then when it hits 2 or 2.1 , the text will show 2. To explain a little more, please check my comment below I hope that kinda explains why it's frustrating that it's round my numbers on screen when the number is still lower

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 Owen-Reynolds · Mar 13, 2016 at 06:36 AM 0
Share

That's what "F0" is supposed to do. It's regular C#, so you can look up examples.

It would help in the future if you wrote the exact line of code.

avatar image Shiro_Rin Owen-Reynolds · Mar 13, 2016 at 07:22 AM 0
Share
 public float number;
 public Text numberText;
 
 
 public void Start(){
 numberText.text = number.ToString('f0");
 }

That's not the actual code, but example of it. I've tried ("n"), ("0"), and plenty of other things. If I put ("f1") then it keeps my 1 at one till it reaches 2, but shows a decimal. If I use ('f0") then once it hits 1.5, it changes the text to 2. Is there a way around this ?

avatar image Positive7 · Mar 13, 2016 at 12:49 PM 0
Share

Use Split:

     string[] array = number.ToString().Split(new char[]
                 {
                     '.'
                 });
             numberText.text = array[0].PadLeft(1, '0');


1 Reply

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

Answer by jgodfrey · Mar 13, 2016 at 03:34 PM

Sounds like you want:

  Mathf.Floor(number).ToText();
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 Shiro_Rin · Mar 13, 2016 at 06:50 PM 0
Share

"Returns the largest integer smaller to or equal to f" That is exactly what I need, thank you so much !!

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

39 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

Related Questions

Why are my float rounding themselves out to the lowest int without me coding it out? 1 Answer

NullReferenceException: Object Reference not set to an instance of an object 1 Answer

Error CS0266 with my clicker game 1 Answer

Is this a glitch??? My attackCooldown float rounding to 1, only when I use a variable. Works if I hard code in the value 1 Answer

Make a float display 10:07 instead of 10.7 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