- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert variables from one type to another in Arduino
In order to convert variables from one type to another, you use the CAST operator. The syntax is −
(type) var;
Where var is the variable to be casted, and type is the new type to which you wish to convert it. For example, if you have a variable of type float, and wish to cast it as an int.
Example
Here’s how you can do it −
float f; int i; void setup() { // put your setup code here, to run once: f = 5.6; i = (int) f; Serial.println(f); Serial.println(i); } void loop() { // put your main code here, to run repeatedly: }
The Serial Monitor will print 5.6 followed by 5 (and not 6). This is because casting a float to int truncates the float, doesn’t round it.
Advertisements