如何根据小数位数获取整数的动态最大值

时间:2018-10-04 23:37:21

标签: c# math logic

我的动态数据为0001,因为它是动态的,所以可以根据给定的数据进行更改。它可以是01或001或0001。要得出此值,我的代码为

            var placeholderValue = 4; //number can be dynamic. 
            var intFormattedValue = String.Format($"{{0:D{palceholderValue}}}", 1);
            var parasedValue =  Int32.Parse(intFormattedValue); //output 0001

如果上述placeholderValue为4,则输出为0001,如果为3,则为001,然后为2 01。

我想要得到的是如果值是0001,那么它将返回9999 如果值为001,则为999;如果值为01,则为99。

2 个答案:

答案 0 :(得分:4)

您可以尝试使用Math.Pow方法获得10次幂,并进行一些计算以获得最大值

Math.Pow(10, placeholderValue) - 1

答案 1 :(得分:0)

直接创建一个新字符串

string placeHolderValue = "0001";
string output = new string('9',placeHolderValue.Length);
//if an int is needed
int outInt = Int32.Parse(output);