# R float to integer: How to Convert Double to Int in R

In the R language, the two most common numeric classes are integer and double (It is used for double-precision floating-point numbers). To create a numeric vector using the c() function, it will produce a vector of double-precision numeric values.

## R float to integer

To convert a float or double to integer in R, use the as.integer() function. The as.integer() is an inbuilt function used for an object of class ursaRaster that truncates the decimal part of image values and then converts to type integer. To create or test for objects of type “integer”, use the integer() function.

### Syntax

``as.integer(x, ...)``

### Parameters

The is the ursaRaster object, and the is other arguments.

### Example

``````r_float <- 11.21
print(r_float)
print(typeof(r_float))

r_int <- as.integer(r_float)
print(r_int)
print(typeof(r_int))``````

#### Output

`````` 11.21
 "double"
 11
 "integer"``````

In this example, first, we defined a double variable and then printed its data type. Then use the as.integer() function to convert the double variable into an integer.

To see if the variable is an integer in R, use the is.integer() function.

``````r_float <- 11.21
r_int <- as.integer(r_float)
print(is.integer(r_int))``````

#### Output

`` TRUE``

By default, if you read in data that has no decimal points or you create numeric values using, for example, the x <- 1:5 method, the numeric values will be coded as an integer.

## Convert integer to double in R

To convert an integer to double in R, use the as.double() method. The as.double() is an inbuilt R function that converts an integer to the double class.

``````r_int <- 11
r_float <- as.double(r_int)
r_float
typeof(r_float)``````

#### Output

`````` 11
 "double"``````

## Integer vs. Double in R Language

There are multiple classes that are grouped together as “numeric” classes and the most standard of which are double (for double-precision floating-point numbers) and an integer.

R will automatically convert between the numeric classes when needed, so for the most part it does not matter to the casual user whether the number 3 is currently stored as an integer or as a double. Most math is done using double-precision, so that is often the default storage.

## Conclusion

To convert from integer to double, use the as.double() method and to check the double value, use the is.double() method.

To convert from double to integer, use the as.integer() method and to check the integer value, use the is.integer() method.

Categories R