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
1
Question by Steve Edwards · Jul 09, 2010 at 12:02 PM · formatprint

How to format numbers for print()... is javascript different to C?

Hi

I couldn't find anything in the docs about using the print() function. I googled for a javascript reference which said that javascript print() uses the same syntax as for C printf(), but that doesn't seem to work in Unity. Can anyone point me to some docs for numeric precision syntax (%.2f etc.)?

In a nutshell, how would I format this C statement for Unity?

printf("%s is %d and thinks pi = %.2f", "Dave", 42, 3.14159);

( Should print: Dave is 42 and thinks pi = 3.14)

Thanks

Steve

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 spinaljack · Jul 09, 2010 at 12:06 PM 0
Share

I think if you write number.ToString(2f) it'll turn the number into a string with 2 decimal places

avatar image Steve Edwards · Jul 09, 2010 at 12:17 PM 0
Share

Thanks for the suggestion, but it still gives a syntac error.

avatar image Steve Edwards · Jul 09, 2010 at 12:18 PM 0
Share

LOL, "syntac error", I meant "syntax error"

1 Reply

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

Answer by Mike 3 · Jul 09, 2010 at 12:25 PM

print only takes a single string. If you want more printf style input, use String.Format like this:

print( String.Format("{0} is {1} and thinks pi = {2:F2}", "Dave", 42, 3.14159) );

Documented here: http://msdn.microsoft.com/en-us/library/b1csw23d.aspx

And info on numeric formatting for it here: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

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 Steve Edwards · Jul 09, 2010 at 12:41 PM 0
Share

Thanks so much, exactly what I needed. Would never have thought of looking on msdn.

avatar image Mike 3 · Jul 09, 2010 at 12:48 PM 0
Share

Fun hmm? Unity's javascript is basically just a javacript-ish .NET implementation, so it has full access to the majority of .NET

avatar image Steve Edwards · Jul 09, 2010 at 01:55 PM 0
Share

Good to know, I'll have a root around see what else I can find.

avatar image fireDude67 · Dec 06, 2010 at 02:23 AM 0
Share

The entire $$anonymous$$SDN Library is why I use C# and not JavaScript

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

No one has followed this question yet.

Related Questions

Export objects to a .3DS file at runtime 1 Answer

Finding Closest Enemy... 2 Answers

Is it possible to detect console prints? 1 Answer

Unity's Equivalent of Console.Write(); 1 Answer

print the mass values of rigidbody attached with the gameobjects assigned to the array variable 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