Mike Jones
Mike Jones

Reputation: 63

converting int to decimal choosing where to put decimal place

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);
}

Upvotes: 4

Views: 4478

Answers (5)

JonH
JonH

Reputation: 33143

Since you are trying to make the number smaller couldn't you just divide by 10 (1 decimal place), 100 (2 decimal places), 1000 (3 decimal places), etc.

Notice the pattern yet? As we increase the digits to the right of the decimal place we also increase the initial value being divided (10 for 1 digit after the decimal place, 100 for 2 digits after the decimal place, etc.) by ten times that.

So the pattern signifies we are dealing with a power of 10 (Math.Pow(10, x)).

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);

Going the opposite direction

You could also do the opposite if someone stated they wanted to take a decimal like 19.56 and convert it to an int. You'd still use the Pow mechanism but instead of dividing you would multiply.

double d=19.56;
int powBy=2;
double n = d*Math.Pow(10, powBy);

Upvotes: 11

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You can try create decimal explictly with the constructor which has been specially designed for this:

public static decimal IntToDecConverter(int number, int precision) {
  return new decimal(Math.Abs(number), 0, 0, number < 0, (byte)precision);
}

E.g.

Console.WriteLine(IntToDecConverter(2423, 2));
Console.WriteLine(IntToDecConverter(1956, 3));

Outcome:

24.23
1.956

Upvotes: 7

user1672994
user1672994

Reputation: 10839

Convert your method like as below

public static decimal IntToDecConverter(int number, int precision)
{
   return = number / ((decimal)(Math.Pow(10, precision)));
}

Check the live fiddle here.

Upvotes: 0

CrisisWhatCrisis
CrisisWhatCrisis

Reputation: 11

number/percisionNumber will give you an integer which you then convert to decimal.

Try...

return Convert.ToDecimal(number) / percisionNumber;

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61339

Moving the decimal point like that is just a function of multiplying/dividing by a power of 10.

So this function would work:

decimal IntToDecConverter(int number, int precision)
{
    // -1 flips the number so its a fraction; same as dividing below
    decimal factor = (decimal)Math.Pow(10, -1*precision)
    return number * factor;
}

Upvotes: 1

Related Questions