1.06 is not a float. The closest float is 1.059999942779541015625. If you multiply that by 100 you get a number that, again, is not a float. Rounding to the nearest float yields 105.99999237060546875, which is the result of the expression ver * 100, and just one ULP short of 106. Yes, floating point computations do quite usually involve rounding steps.

Casting to an int implicitly rounds towards zero, and you get 105, which is the correct result. Correct according to the rules of IEEE 754 arithmetics.

You may want to round() to the nearest integer before the cast.

Variable ver can be any decimal in this format x.xx

No, a float cannot be “any decimal” in this format. Floats are stored in binary, and most decimal numbers (numbers that can be written with a finite number of decimal digits) cannot be written in binary with a finite number of bits.

Except for a few values (namely the multiples of 0.25), ver can only store an approximation of the decimal number.

Answer from Edgar Bonet on Stack Exchange
🌐
Arduino Forum
forum.arduino.cc › projects › programming
converting a float to integer - Programming - Arduino Forum
August 26, 2017 - Is it possible to write a function that gets float values and convert them to integers?
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to actually convert float to int? - Programming - Arduino Forum
October 28, 2023 - int(float) returns wildly wrong values... I get that this is some kind of conversion error to be expected, I'm working with a float like 12345.67 and it comes out to weird numbers. ...but I really need to get an int fro…
Discussions

Converting float to int
Hi guys, Im building an android program in processing and I need some help. I would of postid this in the processing forum but it should be an easy question and nobody is there?!!? Also processing language is very simil… More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
January 25, 2012
Convert float to int error - Arduino Stack Exchange
I am trying to convert 1.06 to 106 by multiplying by 100 but the answer comes 105, I do not know why. Variable ver can be any decimal in this format x.xx float ver = 1.06; int vers = ver * 100; vm[... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
September 30, 2021
Converting float to int by removeing decimal point
I'm working on a project where I need to convert a float to an integer so that I can send it over serial. I want to convert it so that float 12.34 becomes int 1234 then on the other end, I can just divide int 1234 by 100 and get float 12.34 More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
December 16, 2021
arduino - C embedded convert float to int - Stack Overflow
Hello I am trying get to work a simple 5hz led via interrupt on an arduino. I want to calculate how many Timer overflows have to occure before I have to toggle my leds. However I cant compute the v... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Convert float to Int - Programming - Arduino Forum
February 1, 2021 - I have a variable called maxVoltage, say maxVoltage = 1.95. How can i convert this to 195, in essence i want to "remove" the decimal point. If i multiply by 100 int maxVoltageInt = maxVoltage * 100; Serial.print("max Volage is now: "); Serial.println(maxVoltageInt); I get 196 in the output
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Converting float to int - Programming - Arduino Forum
January 25, 2012 - Hi guys, Im building an android program in processing and I need some help. I would of postid this in the processing forum but it should be an easy question and nobody is there?!!? Also processing language is very simil…
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Converting float to int by removeing decimal point - Programming - Arduino Forum
December 16, 2021 - I'm working on a project where I need to convert a float to an integer so that I can send it over serial. I want to convert it so that float 12.34 becomes int 1234 then on the other end, I can just divide int 1234 …
Find elsewhere
🌐
GitHub
github.com › arduino › Arduino › issues › 8751
Converting float to int · Issue #8751 · arduino/Arduino
April 4, 2019 - float answer = 0.0; float i = 0.0; void setup() { Serial.begin(115200); Serial.println("Hello World !!!"); } void loop() { while(true) { answer = pow(5.0, i); Serial.print("Answer: "); Serial.println(answer); if (i >= 4.0){i = 0.0;}; //preventing overflow int b = static_cast<int>(answer);//converting to int Serial.print("B: "); Serial.println(b); delay(500); i += 1.0; } }
Author   mariusvds
🌐
Arduino
arduino.cc › reference › en › language › variables › data-types › float
float | Arduino Documentation
May 15, 2024 - Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure · For controlling the Arduino board and performing computations
🌐
TutorialsPoint
tutorialspoint.com › convert-string-to-integer-float-in-arduino
Convert string to integer/ float in Arduino
March 24, 2021 - void setup() { Serial.begin(9600); Serial.println(); // put your setup code here, to run once: String s1 = "235"; String s2 = "1.56"; String s3 = "Hello"; int i1 = s1.toInt(); int i2 = s2.toInt(); int i3 = s3.toInt(); float f1 = s2.toFloat(); float f2 = s3.toFloat(); Serial.println(i1); Serial.println(i2); Serial.println(i3); Serial.println(f1); Serial.println(f2); } void loop() { // put your main code here, to run repeatedly: } ... As you can see, the integer and float conversions for "Hello" are 0.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Float-to- int failure - puzzlement! - Programming - Arduino Forum
September 6, 2023 - Hi all. I am using Arduino IDE and ESP32. The code below is showing an incorrect result in the last Print statement. I suspect it is the conversion from float to int the problem. Please can you see why is this happening? Regards float Vin = 1; void setup() { Serial.begin(9600); print_int(); } void print_int() { Vin = Vin * 0.1; //Here Vin=0.1 int int_1 = Vin * 1000; Vin = Vin * 0.1; //Here Vin=0.01 int int_2 = Vin * 1000; Vin = Vin * 0.1; //Here Vin=0...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Convert Float to Int Problem - Programming - Arduino Forum
January 7, 2021 - Essentially, I multiplied two floats successfully then just need them to convert into an int so I can put them into the stepper function. But the resulting integer flips it negativity and changes the value. distX=14.00 stepX=-29976 stepXfloat=35560.00 speedX=600 Beep Boop The "stepXfloat is ...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
float vs integer - Programming - Arduino Forum
March 16, 2017 - what is the result of "float"/"integer"? A float or an integer? What, if the second is true, should I do to obtain a float while the divisor is a variable "int x = 8"?
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › international › deutsch
float in int umwandeln - Deutsch - Arduino Forum
May 28, 2010 - Hallo, wie kann ich ein float in ein int umwandeln? Oder wie kann ich eine Kommazahl in eine Ganzzahl auf oder abrunden. Ich habe schon im Forum gesucht, konnte aber nicht das Richtig finden. Erschwerend kommt hinzu d…
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Transform int to float - Programming - Arduino Forum
February 3, 2018 - Hi, I would like to transform an int to a float but changing the decimal points. Example: int a=1235; float x=0; I would like to store a on x such that I have x=12.35; I won't explain why i need to do something lik…
🌐
Arduino Forum
forum.arduino.cc › projects › programming
[FIXED] Problem converting float to int - Programming - Arduino Forum
March 31, 2016 - Hello, I am working on a project, where I get an imput from a force sensitive resistor and want to convert "float force" into "int b" to farther use b in my programm. I am using SEN-09376. This is what I have atm: float force; int b=(int)force; const int FSR_PIN = A0; const float VCC = 4.98; const float R_DIV = 3230.0; void setup (){ Serial.begin(9600); pinMode(FSR_PIN, INPUT); } void loop() { int fsrADC = analogRead(FSR_PIN); if (fsrADC != 0) { float fsrV = fsrADC * VCC ...
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Converting Float to Int - General Guidance - Arduino Forum
March 3, 2013 - Hey forum, Im building a gps reciever. I allready recieve coordinates, but I want to display these coordinates on an PCD8544 lcd screen. I recieve the coordinates as a float, example: flat=51.96512; This coordinate…
Top answer
1 of 6
5

An integer is a whole or natural number. Computers use integers for counting and comparisons. Computers can count precisely using integers and comparisons of two integers can be absolutely true or false.

To make a float or floating-point number, a computer uses two integer numbers. It uses one integer as the mantissa and the other as the exponent. Computers use floats for calculations involving very large, very small or fractional values. In other words the set of real numbers. Computers can not count very well using floats. Computer can not even compare two floats very well. That is to say, you may get different results from different computers when testing if two different floats are the same or not. This is due to rounding error.

Consider that the set of real numbers between 0 and 1 to be infinite. However, intuitively, we know computers can not keep track of an infinite number of unique values between 0 and 1. After all, computers use a float to do this and a float has a finite number of bits in it's mantissa and exponent. The difference, if any, between the actual real number from, say, a calculation and the next possible float value is commonly referred to as the roundoff error. In a program, a difference, for example, in the order of operation is enough to cause different roundoff errors to occur resulting in two slightly different float value results.

Rounding errors are what we end up with for allowing float values to grow very large or very small or to contain factional amounts. Fortunately, floats are exactly what we need when computers are asked to calculate very large, very small or fractional numbers.

In embedded programming we rarely use floats. They take up more of our limited RAM space than integers. And it is rare that we need to calculate very large or very small numbers. Usually, in embedded programming, we are counting events or measuring sensors. Usually integers are preferred or are sufficient for our purposes.

Addressing your specific application (pedometer):

It is anticipated you would prefer to track distances in miles. And it is practical to assume a good pedometer displays factional miles. You can use floats to calculate this value. A rigorous programmer would cast their integers as floats when mixing them in equations to make it clear to the compiler what is desired.

2 of 6
3

"int" means integer. It's either one whole number (forgive me, mathematicians, for the sloppy definition!) or the next or previous.

A "float" is a floating-point number - that is, a number and part of a number.

3 is an int

3.14 is a float

If you need to store/represent a value that can be between integers, you could use a float.

Floats use more RAM than integers, and there is a limit to the precision they can represent. Don't use a float unless you have a need.

🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
Casting Float to Int - Syntax & Programs - Arduino Forum
July 22, 2010 - Hello all, Been playing around with formating float to a string for my specific needs but encountered a problem. Here is the complete sketch I am testing with: #include // Include the Streaming library #include 0 ) value += 0.5/(float)100; el...