Custom string formatting: ToString(“00”)

痞子三分冷 提交于 2019-12-13 22:57:00

问题


Given the following code:

string istanbul = "523";
Convert.ToInt32(istanbul.ToString("00"));

what does it return?


回答1:


The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero after the decimal point determines the range of digits that are always present in the result string.

The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.

The "0" Custom Specifier link text




回答2:


This will not even compile:

string istanbul = 523

You cannot assign a number to a string variable like that. You also did not terminate the statement properly with a ;.

C# is also case sensitive, so istanbul and Istanbul refer to different variables.

To answer the question:

523.ToString("00"); // This will evaluate to the string "523"
Convert.ToInt32("523"); // This will evaluate to the integer 523

Read about custom numeric formatting strings.



来源:https://stackoverflow.com/questions/2779206/custom-string-formatting-tostring00

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!