Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
avatar image
0
Question by UpheavalEntertainment · Jun 03, 2017 at 01:58 AM · stringdecimalcomma

How to add more commands in the ToString();

I want to have a large number with no decimals and commas. i know i can do ToString("n2"); for commas and ToString("0f"); for no demials. how do i put both in?

Example ToString("n2",0f"); (doesnt work)

Comment
Add comment · Show 1
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 Fanttum · Jun 03, 2017 at 03:25 AM 0
Share

Have you tried ToString("n2") + ToString("0f"); ? Not quite sure what the expected outcome you are looking for

2 Replies

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

Answer by Bunny83 · Jun 03, 2017 at 08:09 AM

Those aren't "commands" but format descriptors. You probably just want to use "D". However "D" only works with integral types (byte, short, int, ...) but not with floating point numbers (float, double, ...). However if you have a floating point number you can cast it to "int" or "long" before you call ToString.

Though the format "F0" should work as well. F0 has no 1000th-commas and no decimal digits.

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 UpheavalEntertainment · Jun 03, 2017 at 11:50 AM 0
Share

D only removes decimals and doesnt add comas, does the same. is there no way to combine both or something, also thanks for the strong reply

avatar image Bunny83 UpheavalEntertainment · Jun 03, 2017 at 01:19 PM -1
Share

Uhm you said you want a

number with no decimals and commas

So no decimals and no commas. If you wanted to add commas you should have worded that sentence different.

If you just want to remove decimals but want group seperators you can simply use "N0". Or as i said in my answer you can simply cast your floating point number into a "long" for example. long doesn't have any decimal digits.

 // C#
 float someNumber = 123456.789f;
 long num = (long)someNumber;
 string s = num.ToString("N");

or simply

 // C#
 string s = ((long)someNumber).ToString("N");
avatar image UpheavalEntertainment Bunny83 · Jun 03, 2017 at 08:25 PM 0
Share

Yes thankyou, "N0" works! sorry to waste ya time'

avatar image
0

Answer by FM-Productions · Jun 03, 2017 at 12:54 PM

You could try using the string.Format function

 float variable1 = 13f;
 int variable2 = 14;
 bool variable3 = false;
 
 string formattedStr = string.Format("My float = {0}, my int = {1}, my bool = {2}", variable1, variable2, variable3);

For information about formatting floats etc. with specified decimal digits, look here:

https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

for example string.Format("{0:F0}", myFloatVariable); should do the trick for floats without decimal digits. I haven't testet it though.

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 UpheavalEntertainment · Jun 03, 2017 at 08:26 PM 0
Share

ah yeah, but im just going easy and use "N0" but thanks for da reply

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

67 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

Related Questions

converted array item to string returns "s,tri,ng" with commas? 1 Answer

Unity changing decimal points to decimal commas? 1 Answer

Help with strings and decimals 1 Answer

Change InputField decimal mark 1 Answer

Mobile device change input field (comma/point) 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