convert a decimal to an int32 c#

Page 1 of 1

2 Replies - 11431 Views - Last Post: 13 May 2008 - 05:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=51925&amp;s=2a59f3b58594e1ab471033958cf95dd1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Christianne

• New D.I.C Head

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

convert a decimal to an int32 c#

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

Is This A Good Question/Topic? 0

Replies To: convert a decimal to an int32 c#

#2 zakary

• D.I.C Regular

Reputation: 27
• 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

Quote

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.

#3 thor78

Reputation: 1
• 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.