C# PadLeft 不起作用 [英] C# PadLeft not working
问题描述
我在从 SQL 获取数字字符串并将其放入 excel 时遇到问题,但 excel 格式化数字时没有前导零.
I have an issue with taking a number string from SQL and putting it into excel, but excel formats the number without it's leading zeros.
我写的另一个程序读取excel并将其放入pdf.在这第二个程序中,我决定重新添加丢失的前导零.
Another program I wrote reads the excel and put it into a pdf. It is in this second program I decided to reappend the missing leading zeros.
我正在使用 .PadLeft 来执行此操作,但遇到了一些错误.为了文档起见,我将把我发现的这个问题添加到 SO 并回答它.这是我的问题:
I am using .PadLeft to do this and ran into some errors. For documentation sake, I am going to add this issue I found to SO and also answer it. Here was my problem:
我需要数字字符串为 10 位数字,前面有零,最多 10 位数字.
I need the number string to be 10 digits, with zeros on the front up to 10 digits.
我有来自 excel 的 77776666 之类的数字,它们应该是 0077776666(10 位数字)这是不起作用的.它没有附加任何零:
I have numbers like 77776666 coming from excel, they should be 0077776666 (10 digits) Here is what DIDN'T work. It did not append any zeros:
string insuranceID = Convert.ToString(xlRange.Cells[i, 21].Value2 ?? "");
insuranceID.PadLeft(10, '0');
推荐答案
为了使这段代码工作,我实际上不得不像这样编写代码:
To make this code work, I actually had to write the code like so:
string insuranceID = Convert.ToString(xlRange.Cells[i, 21].Value2 ?? "");
insuranceID = insuranceID.PadLeft(10, '0');
它必须在 .PadLeft 位的前面有代码的赋值部分.
It had to have the assignment part of the code on the front of the .PadLeft bit.
我没有在 SO 上找到答案,只是发现了我的答案.我希望这可以帮助别人.
I didn't find an answer to this on SO and just discovered my answer. I hope this helps someone.
这篇关于C# PadLeft 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!