# Transform int to float

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 like that... I know it isn't pretty :(. I still hope somebody might help with this. Thanks.

Bastian

What happens if you divide a by 100 and assign the result to x ?

Divide by 100.0. Or am I missing something?

you can also multiply by 0.01 (which is in fact faster than division on Arduino)

``````void setup()
{
Serial.begin(115200);
int a =1235;
float x = a * 0.01;
Serial.println(a);
Serial.println(x, 2);  // print with 2 decimals
}

void loop()
{
}
``````

So you make

x = float( a ) / 100.0; // moves decimal place 2 left

The float() command turns what it is given into a float. That is from the Arduino Reference Page, linked on the RESOURCES pulldown menu in the green bar at the top of your forum page, just look up to see it.

I thought that dividing by 100.0 would "promote" the variable 'a' to a float automatically.

CtrlAltElite: I thought that dividing by 100.0 would "promote" the variable 'a' to a float automatically.

No, it wouldn't promote it. But it would ensure that the answer came out a float. In @GFS's example the (float) isn't needed.

The usual arithmetic conversions state if either operand has a floating-point type, then the operand with the lower conversion rank is converted to a type with the same rank as the other operand.

CtrlAltElite: I thought that dividing by 100.0 would "promote" the variable 'a' to a float automatically.

It does. I put that function in for clarity same as at times I might use parens that aren't necessary but do show my intent.

Arduino has a whole set of type conversion functions.

Also - if you are just printing the result, rather than doing floating point math with it, dispense with the float entirely.

In a quick test, replacing that:

``````  float y = x/100.0;
Serial.println(y,2);
``````

with this:

``````Serial.print(x/100);
Serial.print(".");
Serial.println(x%100);
``````

reduced the size of my test sketch from 3268 bytes to 1992 bytes (the gains will be much smaller if you're doing floating point stuff elsewhere in the sketch - the floating point math is all done in software, so if you have any float-related stuff, and for each different floating point operator you use, it pulls in a bunch of float-related stuff.

Except that that won't work, will it? What about leading zeroes in the fraction?

DrAzzy:

``````Serial.print(x/100);
``````

Serial.print(".");
Serial.println(x%100);

Maybe this would work better:

``````Serial.print(x/100);
Serial.print(".");
if ((x % 100) < 10) {
Serial.print("0");
}
Serial.println(x%100);
``````

Many of the answers here are all very well when it comes to printing the value as a float but the OP originally said

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 like that...

Whether he/she really needs the value in a float variable we really don't know, but that was the original requirement

Bastop: 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 like that... I know it isn't pretty :(. I still hope somebody might help with this. Thanks.

Divide by 100.0, or 100f. Do not divide by 100 - if you do, the compiler will do an integer division because both operands are integers.

Bastop: I won't explain why i need to do something like that... I know it isn't pretty

Is this an XY problem? http://xyproblem.info/

odometer: Is this an XY problem? http://xyproblem.info/

Oh, almost certainly. And if the OP is using floats but wants an accurate fixed-point, they'll encounter the usual problems.