Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dynamic maximum value of integer based on the number of decimal places

Tags:

c#

math

logic

I have a dynamic data of 0001, since it is dynamic it can change based on the data given. it could be 01 or 001 or 0001. to derive this value i have a code of

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

if the placeholderValue above is 4, the output would be 0001 if it is 3 then 001 then if 2 01.

What i want to get is if the value is 0001 then it would return 9999 if the value is 001 then 999 if 01 then 99.

like image 998
Mark Yu Avatar asked Dec 20 '25 03:12

Mark Yu


1 Answers

You can try to use Math.Pow method for 10 power and some calculation get the maximum value

Math.Pow(10, placeholderValue) - 1
like image 168
D-Shih Avatar answered Dec 22 '25 16:12

D-Shih



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!