Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Telamon

C# - Converting a double to a string with limited precision

This topic is 5369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok. Really I''m embarassed to even have to ask, and I definately went through the docs to try to avoid it... If I have a double d = 2.0/9.0, I get a really nasty fraction. I want to convert this double to a string, but only keep the first two decimals. If I were coding in C, I''d simply sprintf with "%.2g", but I don''t know how to do this manipulation in C# the Right Way. Of course there is the hack where you multiply by 100, cast to int, and divide it back - but that''s really ugly. Please educate me! ---------------------------------------- Let be be finale of seem, seems to me. ---------------------------------------- Coding: http://www.stanford.edu/~jjshed/coding Miscellany: http://www.stanford.edu/~jjshed

Share this post


Link to post
Share on other sites
Advertisement
Would that be a string formatting class? Or the weird new C# string format specifiers (why did they change from C/C++?)? What is the sprintf equivalent in C#?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
d.ToString("0.00";

or, if it''s specifically money,

d.ToString("C";

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!