2 Replies - 15617 Views - Last Post: 13 May 2008 - 05:10 PM Rate Topic: -----

#1 Christianne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-April 08

convert a decimal to an int32 c#

Post icon  Posted 13 May 2008 - 09:33 AM

How do you convert a decimal to an int32?

I have a decimal value of 1.67 and wish to convert it to an int retaining its value.

I see examples such as the following:

decimal d = 12.30M;

which is great if you're hardcoding your values.

I'm not.
I have a method which is overriden and generates an int value which I get returned.

protected override int CallSomeMethod()

This method above is used all over the place and up until now it was not necessary to return a complete decimal value. With the int I get a rounded value back instead of the complete decimal value. So what I have started to do is change the return type of the method from int to decimal

protected override decimal CallSomeMethod()

This is now causing me numerous problems as I am now having to change the property datatypes of all my holding objects and will involve extensive testing of all changed code.

Is there an easier way to do this?

Thanks :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: convert a decimal to an int32 c#

#2 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: convert a decimal to an int32 c#

Posted 13 May 2008 - 10:12 AM

int is whole number meaning ....-1,0,1,..... no decimal values, so doing


protected override decimal CallSomeMethod()
is the correct thing to do. now if you want to convert a decimal to an int, the value will round and can be done, but it seems you need the decimal number so you should use decimal.
Was This Post Helpful? 0
  • +
  • -

#3 thor78  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 06-May 08

Re: convert a decimal to an int32 c#

Posted 13 May 2008 - 05:10 PM

You can try:
decimal MyDecimal = 1.23M;
int MyInt = Convert.ToInt32(MyDecimal);

But I don't think this is what you intend to do right? Hmmm... try casting with (int) before ending the method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1