gpt4 book ai didi

C#程序,有没有tostring()哪个好

转载 作者:行者123 更新时间:2023-11-30 13:50:14 26 4
gpt4 key购买 nike

一个C#程序,如你所见,var month定义为int,有人说没有.tostring()更好,应该去掉多余的调用,现在是:str= "0"+ Month; 但我认为这不好。哪个更好?为什么?谢谢!(ps:我在stackoverflow的第一个问题)

string strM = string.Empty;
if ( Month < 10 )
strM = "0" + Month.ToString ( );
//strM = "0" + Month; which is better?

最佳答案

改用字符串格式:

string strM = string.Format("{0:00}", Month);

测试:

Month: 1 => strM: "01"
Month: 12 => strM: "12"

有关更多字符串格式提示,请查看 this .

关于C#程序,有没有tostring()哪个好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com