# converting int to decimal choosing where to put decimal place

• A+
Category：Languages

I have an interesting problem, I need to convert an int to a decimal.

So for example given:

``int number = 2423; decimal convertedNumber = Int2Dec(number,2); // decimal should equal 24.23  decimal convertedNumber2 = Int2Dec(number,3); // decimal should equal 2.423 ``

I have played around, and this function works, I just hate that I have to create a string and convert it to a `decimal`, it doesn't seem very efficient:

``decimal IntToDecConverter(int number, int precision) {    decimal percisionNumber = Convert.ToDecimal("1".PadRight(precision+1,'0'));    return Convert.ToDecimal(number / percisionNumber); } ``

Couldn't you just divide by 10 (1 decimal place), 100 (2 decimal places), 1000 (3 decimal places), etc.

Given an input (number of decimal places) make the conversion based on that.

Example:

``int x = 1956; int powBy=3;  decimal d = x/(decimal)Math.Pow(10.00, powBy); //from 1956 to 1.956 based on powBy ``

With that being said, wrap it into a function:

``decimal IntToDec(int x, int powBy)  {   return x/(decimal)Math.Pow(10.00, powBy);  } ``

Call it like so:

``decimal d = IntToDec(1956, 3); ``