Use Convert.ToInt32 from mscorlib as in

decimal value = 3.14m;
int n = Convert.ToInt32(value);

See MSDN. You can also use Decimal.ToInt32. Again, see MSDN. Finally, you can do a direct cast as in

decimal value = 3.14m;
int n = (int) value;

which uses the explicit cast operator. See MSDN.

Answer from jason on Stack Overflow
Top answer
1 of 13
329

Use Convert.ToInt32 from mscorlib as in

decimal value = 3.14m;
int n = Convert.ToInt32(value);

See MSDN. You can also use Decimal.ToInt32. Again, see MSDN. Finally, you can do a direct cast as in

decimal value = 3.14m;
int n = (int) value;

which uses the explicit cast operator. See MSDN.

2 of 13
69

You can't.

Well, of course you could, however an int (System.Int32) is not big enough to hold every possible decimal value.

That means if you cast a decimal that's larger than int.MaxValue you will overflow, and if the decimal is smaller than int.MinValue, it will underflow.

What happens when you under/overflow? One of two things. If your build is unchecked (i.e., the CLR doesn't care if you do), your application will continue after the value over/underflows, but the value in the int will not be what you expected. This can lead to intermittent bugs and may be hard to fix. You'll end up your application in an unknown state which may result in your application corrupting whatever important data its working on. Not good.

If your assembly is checked (properties->build->advanced->check for arithmetic overflow/underflow or the /checked compiler option), your code will throw an exception when an under/overflow occurs. This is probably better than not; however the default for assemblies is not to check for over/underflow.

The real question is "what are you trying to do?" Without knowing your requirements, nobody can tell you what you should do in this case, other than the obvious: DON'T DO IT.

If you specifically do NOT care, the answers here are valid. However, you should communicate your understanding that an overflow may occur and that it doesn't matter by wrapping your cast code in an unchecked block

unchecked
{
  // do your conversions that may underflow/overflow here
}

That way people coming behind you understand you don't care, and if in the future someone changes your builds to /checked, your code won't break unexpectedly.

If all you want to do is drop the fractional portion of the number, leaving the integral part, you can use Math.Truncate.

decimal actual = 10.5M;
decimal expected = 10M;
Assert.AreEqual(expected, Math.Truncate(actual));
People also ask

How to convert int to float in C?
To convert int to float in C, you can use type casting like (float)num. It changes the integer value to a float and allows decimal representation.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ int-to-float
How to Convert Int to Float in C? 3 Programs
Can we convert integer to float in C program automatically?
Yes, when an int is used with a float in an expression, the compiler automatically promotes it, helping to convert integer to float in C program without manual casting.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ int-to-float
How to Convert Int to Float in C? 3 Programs
Does implicit conversion always work in int to float in C?
Yes, but only in mixed-type expressions. For example, int + float will convert int to float implicitly.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ int-to-float
How to Convert Int to Float in C? 3 Programs
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ binary-decimal-convert
C Program to Convert Binary Number to Decimal and vice-versa
Become a certified C programmer. Try Programiz PRO! ... #include <stdio.h> // function prototype long long convert(long long); int main() { long long n; printf("Enter a binary number: "); scanf("%lld", &n); printf("%lld in binary = %lld in decimal", n, convert(n)); return 0; } // function definition long long convert(long long n) { long long dec = 0; int i = 0, rem; while (n != 0) { // get remainder of n divided by 10 rem = n % 10; // add the rem * (2 ^ i) to dec dec += rem << i; // Using bitwise shift instead of pow // divide n by 10 n /= 10; // increment i ++i; } return dec; }
๐ŸŒ
Quora
quora.com โ€บ What-happens-if-we-assigned-a-number-with-a-decimal-to-an-integer-in-C
What happens if we assigned a number with a decimal to an integer in C? - Quora
Answer (1 of 3): Typeconversion is the process of converting one data type to other. It is generally of two types - 1)implicit(automatically done by the compiler) 2) explicit(user defined..... i. e, done by us) When a decimal number is assigned to int data type...... Implicit conversion takes p...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ chash-program-to-convert-an-int32-value-to-a-decimal
C# Program to convert an Int32 value to a decimal
To convert an Int32 value to a decimal, use the Convert.ToDecimal() method. Int32 represents a 32-bit signed integer. Letโ€™s say the following is our Int32 value. int val = 2923; Now to c
Find elsewhere
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
Integer to Decimal Conversion - Programming - Arduino Forum
January 7, 2020 - So, for example, when the int binary is (1100001), corresponding to 97 in decimal, or character (a) as a symbol, it is converted into 95 instead. As another example, I tried (1111000), which corresponds to 120, I get the conversion output to 116. I am attaching the full code below, can someon...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-binary-to-decimal
Convert Binary to Decimal in C - GeeksforGeeks
Update the decimal value by multiplying last_digit with the current base value and adding it to dec. Update the base value by multiplying it by 2 to represent the next power of 2 for the next digit.
Published ย  July 23, 2025
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ int-to-float
How to Convert Int to Float in C? 3 Programs
October 17, 2025 - Learn 3 simple ways to convert an int to a float in C using type casting, implicit conversion, and format specifiers. Includes examples, output, and explanations. Read now!
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 66963-new-cplusplus-decimals-integers.html
New to C++: Decimals to Integers? - C Board
... <cmath> is described in the C++ standard (which means C++ compilers and libraries support it). The std::nint() function is declared in that header. Alternatively, you can use the <math.h> header from C (which C++ is backward compatible to) and the nint() function in that.
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
Convert right of decimal point to integer - Programming - Arduino Forum
July 28, 2020 - I have a float value that I would like to convert what is on each side of the decimal point to an int. Example C = 234.16 coverts to two int values. A = 234 B = 16 Can someone help me with this?
Top answer
1 of 2
4

Use the strtoXXX() family of functions. If you need int, long or long long or their unsigned variants:

long l = strtol("1234567", NULL, 10);
long long ll = strtoll("1234567", NULL, 10);
unsigned long l = strtoul("1234567", NULL, 10);

If you need a float, double, or long double use this:

float f = strtof("3.1415927", NULL);
double d = strtod("3.1415927", NULL);

Manuals here and here.

2 of 2
1

Usually given a string:

char * myStr= "123";

the way to obtain it's value as an int is:

int value=atoi(myStr);

Some things important to notice:

the following include is necessary:

#include <stdlib.h>

and you must be sure that your string is a null terminated string otherwise atoi will crash you program.

You didn't gave us much information but if you're programming a microcontroller (I suspect that since you told us about a motor) you maybe won't want to use stdlib. In that case you might have use a costum function.

Please take a look at the code bellow:

int stringToInt(char* nrStr){
int nrChars=0;
while(nrStr[nrChars]!='\0'){
    nrChars++;
}


int result=0;
int i=0;
while(nrStr[i]!='\0'){//while you dont get to the end of the string
    int digit=nrStr[i]-48;//48 is zero ascii code
    int exp=nrChars-i-1;
    int add=digit*power(10,exp);
    result+=add;
    i++;


}
return result;


}
int power(int base, int exp){
int ret=1;
int i;
for(i=0;i<exp;i++){
    ret*=base;
}
return ret;
}

This does not use any library functions and does the job. I've done it in 3 minutes and it may have some small error, it's not very efficient and does not verify possible errors, but in principle if you pass the strinToint function a well formed integer as a null terminated string it will output the correct value.

If you're using a library that does have some implementation of a power function do use it instead of the one I gave you since it is not efficient at all.

One last note: if you for some reason need to use it in other basis lets say octal basis, you have to chance the line:

int add=digit*power(10,exp);

to:

 int add=digit*power(8,exp);

for hexadecimal this will not work, and implementation of such a function will be significantly different.

๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ float-to-int
How to Convert Float to Int in C? 4 Programs
October 17, 2025 - Learn how to convert a float to an int in C with 4 simple programs. Explore different methods using type casting, floor(), ceil(), and round().
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [c] how can i store a decimal number within an integer variable?
r/learnprogramming on Reddit: [C] How can I store a decimal number within an integer variable?
April 16, 2020 -

Hi! I'm learning to use operators and my handoobk used this program to explain them:

#include <stdio.h>

int main(void) {

/* Variables */

int a =100; b =2; c =25; d =4; result;

result = a - b; //substraction

printf ("a - b = %i\n", result);

}

But what if I want to operate result= c/b and then display it? How can I store the result of 25 / 2 in a variable that has been defined as an int type? Changing the variable type to floator doublemid program resulted in an error, and changing its type throughout the program displayed a 0 on all the operations.

๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2910424 โ€บ how-to-add-integer-part-of-the-decimal-numbers-by-c-language
how to add integer part of the decimal numbers by c language | Sololearn: Learn to code for FREE!
Your code seem to be correct i ... given constraints or not Like this If( numb1>=0&&numb1<=500) ... You need to use modf() function from <math.h>. The function's purpose is to break a floating point value into integral ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-decimaltoint32-method-in-c-sharp
What is the Decimal.ToInt32() method in C#?
... Lines 11-14: We create variables d1, d2, d3, and d4 of type decimal and assign decimal values to them. Lines 18-21: We convert each value to a signed 32-bit integer using the Decimal.ToInt32() method and print the results.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_type_conversion.php
C Data Type Conversion
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT SWIFT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST ... Create Variables Format Specifiers Change Values Multiple Variables Variable Names Real-Life Examples Code Challenge C Data Types ยท Data Types Characters Numbers Decimal Precision Memory Size Real-Life Example Extended Types Code Challenge C Type Conversion C Constants C Operators