我在C#中有一个字符串变量,它将是“ .11”或“ 0.11”或“ 12.11等”字符串格式的“单个”数据类型数字。
我的问题:如果在上述变量的小数点前未指定数字,则需要在前面的字符串前加'0'开头。我将如何以最有效的方式在C#中执行此操作?因此,如果变量为“ 12.11”,则无需填充。
string newValue = null;
string searchFieldValue1 = ".11";
//set new value according to this logic: pad it with a leading 0 so its 0.11
newValue = searchFieldValue1.StartsWith(".")
? "0" + searchFieldValue1
: searchFieldValue1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句