Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

easy way to format numbers to strings?


  • Please log in to reply
1 reply to this topic

#1 BobLewiston

BobLewiston

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:07:55 AM

Posted 02 March 2009 - 12:08 PM

In C#, I'd like to convert some numbers to strings in a standard format, but preferably in a simple way, such as if you were going to output them rather than store them in string variables, a la:
Console.WriteLine ("{0:F2}", num);
rather than use a bunch or string methods to make them all the same number of significant digits. Isn't there any way to do this?

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:55 AM

Posted 02 March 2009 - 12:50 PM

static void Main(string[] args)
		{
			Console.Write(String.Format("{0:0.##}", 123.45698));
			Console.Write("\n");
			Console.Write(String.Format("{0:0.#}", 123.45698));
			Console.Write("\n");
			Console.Write(String.Format("{0:0.###}", 123.45698));

		}

It takes one line (four if you make a method to do it). What are you trying to do?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users