27

I have the following int 7122960
I need to convert it to 71229.60

Any ideas on how to convert the int into a decimal and insert the decimal point in the correct location?

Baxter
  • 5,633
  • 24
  • 69
  • 105

3 Answers3

63
int i = 7122960;
decimal d = (decimal)i / 100;
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • For clarity, it might be worth using `(decimal)i / 100M` to highlight the fact that the division happens after the conversion to decimal (as it must). (As it is, the 100 is being automatically converted to decimal anyway, so it's just a clarity thing.) – Hutch Mar 18 '19 at 23:40
5

Simple math.

double result = ((double)number) / 100.0;

Although you may want to use decimal rather than double: decimal vs double! - Which one should I use and when?

Community
  • 1
  • 1
yoozer8
  • 7,361
  • 7
  • 58
  • 93
4

Declare it as a decimal which uses the int variable and divide this by 100

int number = 700
decimal correctNumber = (decimal)number / 100;

Edit: Bala was faster with his reaction

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
dennis
  • 2,000
  • 18
  • 26