How to format a decimal in C# with at least one digit after the decimal point, but not a fixed upper limit if specified more than 1 digit after the decimal point:
5 -> "5.0"
5.1 -> "5.1"
5.122 -> "5.122"
10.235544545 -> "10.235544545"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
ToString("0.0###########################").Some notes:,
#s in there, as thedecimalstructure can accommodate precision up to 28 decimal places.0custom specifier will cause a digit to always be displayed, even if the value is 0.#custom specifier only displays a value if the digit is zero and all of the digits to the right/left of that digit (depending on what side of the decimal point you are on) are zero.#after the first0to the right of the decimal point to accommodate the length of all the values you will pass toToString, if you will only have precision to 10 decimal places, then you need nine#(since you have the first decimal place to the right handled by0)For more information, see the section of MSDN titled “Custom Numeric Format Strings”.