if < 则在数字前加 0 10

发布于 2024-11-30 16:32:04 字数 472 浏览 1 评论 0 原文

这是我的代码:

string Unreadz = "0";
while (true)
{
    Unreadz = CheckMail();       
    if (!Unreadz.Equals("0")) port.Write("m");
    else port.Write("n");
}

如果 Unreadz 小于 10,我想在其之前、port.write< 之前添加一个 0 /代码>。

这是我尝试过的:

if (Unreadz < 10) port.Write("0" + Unreadz);
else port.Write("" + Unreadz);

但它不起作用,因为 Unreadz 是一个字符串。

This is my code:

string Unreadz = "0";
while (true)
{
    Unreadz = CheckMail();       
    if (!Unreadz.Equals("0")) port.Write("m");
    else port.Write("n");
}

If Unreadz is less than 10, I want to add a 0 before it, before port.write.

This is what I have tried:

if (Unreadz < 10) port.Write("0" + Unreadz);
else port.Write("" + Unreadz);

but it doesn't work because Unreadz is a string.

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

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

发布评论

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

评论(10

极致的悲 2024-12-07 16:32:04

这应该有效:

port.Write(Unreadz.ToString().PadLeft(2, '0'));

This should work:

port.Write(Unreadz.ToString().PadLeft(2, '0'));
[旋木] 2024-12-07 16:32:04
string.Format("{0:00}", 9);

另请参阅 http://msdn.microsoft.com/ en-us/library/0c899ak8(v=VS.71).aspx

更直接的是,您可以:

  9.ToString("00"); // "09"
 19.ToString("00"); // "19"
119.ToString("00"); // "119"
string.Format("{0:00}", 9);

See also http://msdn.microsoft.com/en-us/library/0c899ak8(v=VS.71).aspx

More directly you can:

  9.ToString("00"); // "09"
 19.ToString("00"); // "19"
119.ToString("00"); // "119"
┾廆蒐ゝ 2024-12-07 16:32:04

如果您有一个整数,ToString 方法将帮助您:

int i = 5;
string formatted = i.ToString("00");

格式化将为 05
您可以根据需要对任意数量的“0”执行此操作,如果您的结束字符串不同,还可以添加额外的字符:

int minutes = 2, seconds = 3;
int timeSum = minutes * 100 + seconds;
string time = timeSum.ToString("00m00s");

时间将为02m03s

If you have an integer, the ToString method will help you:

int i = 5;
string formatted = i.ToString("00");

formatted will be 05
you can do this for as many '0' as you want, and also put extra characters if your end string is different:

int minutes = 2, seconds = 3;
int timeSum = minutes * 100 + seconds;
string time = timeSum.ToString("00m00s");

time will be 02m03s

素染倾城色 2024-12-07 16:32:04

尝试

if (Convert.ToInt32(Unreadz) < 10) port.Write(string.format("{0}{1}", "0", Unreadz);

或者,

if (Unreadz.length == 1 ) ...

try

if (Convert.ToInt32(Unreadz) < 10) port.Write(string.format("{0}{1}", "0", Unreadz);

Alternatively,

if (Unreadz.length == 1 ) ...
情仇皆在手 2024-12-07 16:32:04

首先,Unreadz实际上应该是一个int,而不是一个字符串

然后你可以写:

        int Unreadz;
        while (true)
        {
            Unreadz = CheckMail(); //Change this to return an int

            string formattedNumber = String.Format("{0:##}", Unreadz.ToString());
            port.Write(formattedNumber); 
        }

First of all, Unreadz should really be an int, not a string

Then you can write:

        int Unreadz;
        while (true)
        {
            Unreadz = CheckMail(); //Change this to return an int

            string formattedNumber = String.Format("{0:##}", Unreadz.ToString());
            port.Write(formattedNumber); 
        }
日记撕了你也走了 2024-12-07 16:32:04

如果保证返回值为字符串表示的整数,那么可以将其转换为数字然后进行比较。

if ( Convert.ToInt32(Unreadz) < 10 ) port.Write("0" + Unreadz);
else port.Write("" + Unreadz);

If its guaranteed that the return value will be an integer represented as string, then you can convert it to a number and then perform comparison.

if ( Convert.ToInt32(Unreadz) < 10 ) port.Write("0" + Unreadz);
else port.Write("" + Unreadz);
西瓜 2024-12-07 16:32:04

您现在可以在插值字符串中指定格式:

string myOutput = $"{myNumber:00}";

You can now specify format in an interpolated string:

string myOutput = 
quot;{myNumber:00}";
臻嫒无言 2024-12-07 16:32:04

您需要将字符串转换为数字以进行小于 10 的测试:

// this will throw an exception if the string does not contain a number
int unreadzNumber = int.Parse(Unreadz);

如果您确定该字符串只包含一位数字且没有其他数字,则可以只测试它的长度是否为 1:

bool isLessThanTen = Unreadz.Length == 1;

您可以在 a 中进行格式化多种不同的方式:

// 1: just stick a zero before it
string formatted = string.Format("0{0}", unreadzNumber);
// 2: use a special format string to use at least two digits
string formatted = string.Format("{0:00}", unreadzNumber);
// 3: pad the string with zeros to the left
string formatted = Unreadz.PadLeft(2, '0');
// 4: manually concatenate with "0"
string formatted = "0" + Unreadz;
// and probably others

我更喜欢第二种,因为它使格式更清晰,更易于维护,并且不需要比较。

所以,你得到:

   string Unreadz = "0";
    while (true)
    {
        Unreadz = CheckMail();       
        int unreadzNumber = int.Parse(Unreadz);
        port.Write(string.Format("{0:00}", unreadzNumber));
    }

You need to convert the string to a number to do the test for less than 10:

// this will throw an exception if the string does not contain a number
int unreadzNumber = int.Parse(Unreadz);

If you are certain that the string contains exactly one digit and nothing else, you can just test if it has length 1:

bool isLessThanTen = Unreadz.Length == 1;

You can do the formatting in a variety of different manners:

// 1: just stick a zero before it
string formatted = string.Format("0{0}", unreadzNumber);
// 2: use a special format string to use at least two digits
string formatted = string.Format("{0:00}", unreadzNumber);
// 3: pad the string with zeros to the left
string formatted = Unreadz.PadLeft(2, '0');
// 4: manually concatenate with "0"
string formatted = "0" + Unreadz;
// and probably others

I prefer the second one because it makes the format clearer and easier to maintain and does not require the comparison.

So, you get:

   string Unreadz = "0";
    while (true)
    {
        Unreadz = CheckMail();       
        int unreadzNumber = int.Parse(Unreadz);
        port.Write(string.Format("{0:00}", unreadzNumber));
    }
亢潮 2024-12-07 16:32:04

如果 Unreadz 是一个字符串,那么要比较它,您必须将其转换为 int。

if (未读z < 10)

if (Convert.ToInt16(Unreadz) < 10)

If Unreadz is a string, then to compare it, you would have to convert it to an int.

if (Unreadz < 10)
to
if (Convert.ToInt16(Unreadz) < 10)

画▽骨i 2024-12-07 16:32:04

尝试定义 int unreadz ,然后使用 port.Write(string.Format("{0:00}", unreadz)

Try to define int unreadz and then use port.Write(string.Format("{0:00}", unreadz)

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