Verifying numeric values

See here.

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 (123 will be "123.0", 12.3456 will be "12.346" (rounds up last digit if necessary) )
	MyTextBox.Text = MyVariable.ToString("0.###");    //Force to min 0 decimal places, max 3 decimal places (123 will be "123", 12.3456 will be "12.346" (rounds up last digit if necessary) )
	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");      //Force to 0 decimal place
	MyTextBox.Text = MyVariable.ToString("0000");      //Force 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

Fixed number of decimal places
	MyTextBox.Text = dMyValue.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture);	//Display to 3 decimal places (1.23 will be displaed as "1.230")
Optional number of 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);
Insert leading zero’s if necessary
	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);
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *