ToString()

	MyTextBox.Text = MyVariable.ToString("0.0");    //Force to 1 decimal place
	MyTextBox.Text = MyVariable.ToString("0.0##");    //Force to min 1 decimal place, max 3 decimal places
	MyTextBox.Text = MyVariable.ToString("#.0##");    //Force to min 1 decimal place, max 3 decimal places, don't display leading 0 if value is 0.#
	MyTextBox.Text = MyVariable.ToString("0");      //For to 0 decimal place
	MyTextBox.Text = MyVariable.ToString("0000");      //For to 4 digits, inserting leading zero's if necessary

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

Display Values With Decimal Places

Force display of value with fixed number of decimal places

	MyTextBox.Text = dMyValue.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture);	//Display to 3 decimal places

When formatting numbers you can use “0” as mandatory place and “#” as optional place.

	String.Format("{0:0.####}", 123.4567);    //Gives: "123.4667"
	String.Format("{0:0.##}", 123.4567);      //Gives: "123.46"
	String.Format("{0:0.##}", 123.4);         //Gives: "123.4"
	String.Format("{0:0.##}", 123.0);         //Gives: "123"

	String.Format("{0:0.0#}", 123.4567)       //Gives: "123.46"
	String.Format("{0:0.0#}", 123.4)          //Gives: "123.4"
	String.Format("{0:0.0#}", 123.0)          //Gives: "123.0"

Display Decimal Value

	MyString = String.Format("{0:D}", MyVariable);
	MyString = String.Format("{0:D6}", MyVariable);	//D6 formats value to 6 digits, inserting leading zero's if necessary

Display Decimal Places

	MyString = String.Format("{0:F}", MyVariable);
	MyString = String.Format("{0:F3}", MyVariable);	//F3 formats value to 3 decimal places, inserting trailing zero's if necessary

Display Hex Value

	MyString = "0x" + String.Format("{0:X2}", MyVariable);		//This adds value formatted as hex with 2 digits and with 0x before it

	MyString = "0x" + String.Format("{0:X}", 123456);	//X formats to hexadecimal
	MyString = "0x" + String.Format("{0:X6}", 123456);	//

Display Binary Value

	sTemp = Convert.ToString(10, 2);
	txtInputs0.Text = sTemp.PadLeft(8, '0');

Display Software Version Typical Solution

	MyString = "V:" + Convert.ToString(FirmVer >> 8) + "." + Convert.ToString(String::Format("{0:D2}", (FirmVer & 0x00ff)));

Display DateTime

Link to all format codes

	MyString = String.Format("{0:yyyy-MM-dd HH:mm:ss}", MyDateTime);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.