We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

qrist0ph on 05/13/08


Tagged

format random


Versions (?)


Format Decimal Places


Published in: C# 


  1. public static void generatoRandomDouble()
  2. {
  3. Random random = new Random();
  4. double r = random.NextDouble() * 100;
  5. // cut of all but 2 decimal places
  6. Console.WriteLine(r.ToString("#0.00"));
  7. // round the doube
  8. r= Math.Round(r, 2);
  9. Console.WriteLine(r);
  10. // use point "." as decimal point
  11. Console.WriteLine(r.ToString(CultureInfo.InvariantCulture.NumberFormat));
  12. }

Report this snippet 

You need to login to post a comment.