简体   繁体   English

如何找到n位数的最小值?

[英]How to find min value of an n digit number?

I have been able to achieve what I am looking for but just want to know whether there is an inbuilt method to do so? 我已经能够实现我正在寻找的东西,但只是想知道是否有一个内置方法这样做?

I have a number say 2665. Now since this is a 4 digit, I need the minimum value of a 4 digit number which is 1000. 我有一个数字说2665.既然这是一个4位数,我需要一个4位数的最小值,即1000。

Similarly if the number is 255, the answer would be 100. 同样,如果数字是255,答案将是100。

I tried this 我试过这个

int len = 2665.ToString().Length;

string str = string.Empty;
for (int index = 0; index < len; index++) 
{
    if (index == 0) 
        str += "1";
    else
        str += "0";
}

This gives correct result of 1000 . 这给出了1000正确结果。 But is there an inbuilt function for this? 但这是否有内置功能?

You can use Pow and power 10 to length of string. 你可以使用Pow和10的字符串长度。 For 1 it will give 1 for 2 it will give 10 etc. 对于1,它将给出1为2,它将给出10等。

var str = Math.Pow(10, len - 1).ToString();

您还可以使用字符串的构造函数String(Char,Int32)来创建所需的零序列。

string s = "1" + new string('0', str.Length-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM