自定义字符串格式:ToString(“00”)

发布于 2024-08-31 04:18:40 字数 122 浏览 0 评论 0原文

给出以下代码:

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

它返回什么?

Given the following code:

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

what does it return?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-09-07 04:18:40

“0”自定义格式说明符用作零占位符符号。如果正在格式化的值在格式字符串中出现零的位置有一个数字,则该数字将被复制到结果字符串中;否则,结果字符串中会出现零。小数点前最左边的零和小数点后最右边的零的位置决定了结果字符串中始终存在的数字范围。

“00”说明符使值四舍五入到小数点前最接近的数字,其中始终使用远离零的四舍五入。例如,使用“00”格式化 34.5 将得到值 35。

“0”自定义说明符
链接文本

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

千仐 2024-09-07 04:18:40

这甚至无法编译:

string istanbul = 523

您不能将数字分配给这样的字符串变量。您也没有使用 ; 正确终止该语句。

C# 也区分大小写,因此 istanbulIstanbul 引用不同的变量。

要回答这个问题:

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

请阅读自定义数字格式字符串

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文