[英]How to get dynamic maximum value of integer based on the number of decimal places
我有一個動態數據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,那么如果值是001,則返回9999,如果值是01,則返回999。
您可以嘗試使用Math.Pow
方法獲得10次冪,並進行一些計算以獲得最大值
Math.Pow(10, placeholderValue) - 1
直接創建一個新字符串
string placeHolderValue = "0001";
string output = new string('9',placeHolderValue.Length);
//if an int is needed
int outInt = Int32.Parse(output);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.