【问题标题】:How to add zeros to the left of a number in C#如何在 C# 中的数字左侧添加零
【发布时间】:2014-05-26 12:02:34
【问题描述】:

我有一段将十进制数转换为基数 3 的代码

 string base3 ;//= new char[textBox1.TextLength];
        for (int t = 0; t < textBox1.TextLength; t++) {
            int ascii =int.Parse( new string(Encoding.ASCII.GetBytes(textBox1.Text.Substring(t,1)).SelectMany(b => b.ToString()).ToArray()));
            base3=(ConvertToBase(ascii, 3));


            textBox1.Text = string.Format("{0:000000}", base3);//for example, a returns 1211 but i nees 001211

所以它回显的数字是 101,它有 3 位数字。但我要回显的是 000101 ,所以它有 6 位数字。即使只有 3 或 4 个有用的数字,将 Decimal 转换为始终为 6 位的 base 3 是我的目标!我该如何解决? 当前代码不起作用,重复的 Q 也无济于事

【问题讨论】:

  • 什么是 ConvertToBase?
  • 最优雅的解决方案可以在这里找到:stackoverflow.com/questions/541098/…
  • @Steve 这是一个从 10 到 3 的函数
  • 上述代码中的右大括号在哪里?

标签: c#


【解决方案1】:
var padded = base3.PadLeft(6, char.Parse("0"));

【讨论】:

  • 现在试试,我认为它需要是一个字符。 see msdn article
  • 太好了,请您标记为正确答案吗?
猜你喜欢
  • 1970-01-01
  • 2016-07-24
  • 2016-02-13
  • 2010-10-03
  • 2011-09-17
  • 2013-04-03
  • 2011-02-11
  • 1970-01-01
  • 2017-11-18
相关资源
最近更新 更多