0

I have an integer (representing seconds) which I'm converting to hours by dividing by 3600. I then store the value in a property (type int). If the value contains a decimal point, I convert it by casting. However, when I try to assign the value to the property, I get an error: "Cannot implicitly convert type 'decimal' to 'int'." Here's my code:

var p = ((Hours.Duration) / 3600.0);
(Hours.Duration) = p;

However,

Hours.Duration =  (Hours.Duration) / 3600

works fine, and rounds to an int. What am I doing wrong?

pyrocumulus
  • 9,072
  • 2
  • 43
  • 53
Premanshu
  • 616
  • 3
  • 14
  • 24
  • Similar question [here](http://stackoverflow.com/questions/501090/c-sharp-how-do-i-convert-a-decimal-to-an-int) – abhinav Nov 25 '11 at 07:04

4 Answers4

2
decimal p = ((Hours.Duration) / 3600);
(Hours.Duration) = p;

you are getting error because p is decimal and Hours.Duration is integer, You cannot assign decimal to int without explicit casting.

(Hours.Duration) = (int)p;

If Hours.Duration is integer, 3600 is also integer, then there will be an integer division, that is your decimal value will be lost. e.g. in integer division 7/2 =3. If you want the answer to be 3.5, then you need to have atleast one decimal number in the division i.e 7.0/2 = 3.5 OR 7/2.0 = 3.5.

Zohaib
  • 7,026
  • 3
  • 26
  • 35
  • if Hours.Duration = 1800, then p = 0.5 ..but how to assign that value to Hours.Duration again. On explicit conversion in line 2 Convert.ToDecimal((Hours.Duration)) = p; The error says, the lhs of an assignment must be a variable, property, indexer. – Premanshu Nov 25 '11 at 07:11
  • You can not store values with decimal in int variable. You need to change type of Hours.Duration. That is you cannot save 0.5 in int variable. @Premanshu – Zohaib Nov 25 '11 at 07:22
  • actually, in the database, we store seconds(int) and display to the user as hours (they may be decimals as well)..Till i was using 1,2 etc for testing.. but on decimal entry it rejects..Anyway many thanks for continuous answering.. – Premanshu Nov 25 '11 at 07:26
  • This conversion is actually not possible..And since i was trying these in my model class (MVC)...So did the changes finally in my view...and it works..cheers.. – Premanshu Nov 25 '11 at 07:44
1

Try:

Hours.Duration = Convert.ToInt32(p);
nc3b
  • 15,562
  • 5
  • 51
  • 63
0

You can use this code:

int vIn = 0;
double vOut = Convert.ToDouble(vIn);

Here is a very handy convert data type webpage for those of others: Convert decimal to int in C#

user2771704
  • 5,994
  • 6
  • 37
  • 38
0

Don't define p as decimal. And the question is, if you want to include also partial hour (e.g. if the result for 4000/3600 would be 1 or 2). So you can write directly

Hours.Duration /= 3600;

or if you want count also partial hour

Hours.Duration = Hours.Duration / 3600 + ((Hours.Duration % 3600 > 0)?1:0);

or if you want correct rounding up

Hours.Duration = Hours.Duration / 3600 + ((Hours.Duration % 3600 >= 1800)?1:0);

  • Thanks @Ivo...since Hours.Duration turn out to be an int, hence adding decimal part would still result in error.Isn't??? Many thanks for the reply though. – Premanshu Nov 25 '11 at 09:07
  • Hours.Duration would be 1, accepted.But what about the decimal part???Ideally 4500 shall result into 4500/3600 = 1.25 so where from are we appending the decimal part(900 seconds leftover post division)??? I think we can't assign decimal values to int types after casting.Any more literature would surely help me. Appreciate your concerns.Thanks Ivo – Premanshu Nov 25 '11 at 13:59
  • If you need to show decimal part, you definitely need to change Hours.Duration type. Or think about hours:minutes:seconds format. – Ivo Peterka Nov 25 '11 at 14:01
  • The variable Hours.Duration is a property of type int. – Premanshu Nov 25 '11 at 14:04
  • Extend your structure for integer values DurationH for hours, DurationM for minutes and DurationS for seconds. Then run `int reminder = Hours.DurationH % 3600; Hours.DurationH /= 3600; Hours.DurationM = reminder / 60; Hours.DurationS = reminder % 60;` and finally show the result in format hours:minutes:seconds This is I think the best way. – Ivo Peterka Nov 25 '11 at 14:07
  • Im allowed to display only hours (upto 2 decimals to be precise). – Premanshu Nov 25 '11 at 14:10
  • In that case change Hours.Duration type to decimal and make sure it is correctly formated when shown (something like {0:F2} in the formatting string) . – Ivo Peterka Dec 09 '11 at 08:43
  • Or use Hours.Duration.ToString("F2",Culture.CurrentCulture) with decimal Hours.Duration – Ivo Peterka Dec 09 '11 at 08:52
  • Thanks Ivo...I would sure check it andd let u know of the results... Appreciate your enthusiasm....Cheers – Premanshu Dec 09 '11 at 12:36