 # Converting Float to Int

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 is the type I want to display on the PCD8544. I have tried to make the float an int bij simply multiplying it bij 100,000. This obviously doesn't work, because the number becomes 5,100,000. Can somebody explain how I can convert an 5 decimal float like: 51.96512 to an int like: 5196512 It is no problem to display 5196512 on the PCD8544, but I cant convert the 51.96512 to 5196512.

Any help is welcome.

Regards,

Leo

5196512

leovanasch: Can somebody explain how I can convert an 5 decimal float like: 51.96512 to an int like: 5196512

5,196,512 can't be converted to an int because that well over the maximum amount that can be stored an int.

Try converting it to a long.

tried to convert it:

flat=51.96512; long flatlong=long(flat)

now it puts out 51

if i do the following:

flat=51.96512; long flatlong=long(flat*100000);

it puts out 19168, which doesnt make any sense

This works fine, incrementing properly.

``````float original = 51.123456;
long converted;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
converted = original * 100000;
}

void loop()
{
Serial.println(converted);
delay (2000);
converted++;
}
``````

Try 100000UL.

Mark

leovanasch: if i do the following:

flat=51.96512; long flatlong=long(flat*100000);

it puts out 19168, which doesnt make any sense

In your example you don't give 'flat' a type . the type should be float than it works fine

Ok i've fixed it!

I'm processing the coordinate as below:

flat=51.96512; long flatlong; flatlong=flat*100000; latProces(flatlong,0,5);

the latProces is declared as following:

void latProces(long d, int x, int y)

this is the function I use to process the coordinate to display it on the PCD8544. before, the variable d was declared as an int and not as a long, I changed this and now it works, stupid mistake.

astrofrostbyte: In your example you don't give 'flat' a type . the type should be float than it works fine

flat was allready declared globally as a float

and of course, thank you all very much!

Can I ask the question how you got a float in the first place? If it came as part of an ASCII message, it never was a float to begin with.

AWOL:
Can I ask the question how you got a float in the first place?
If it came as part of an ASCII message, it never was a float to begin with.

the code is part of the based on an example from the tinygps library:
http://arduiniana.org/libraries/tinygps/

The following piece of code gives the positions:

gps.f_get_position(&flat, &flon, &age);

But TinyGPS reads NMEA sentences - they're ASCII. If it never was a float, there's little point converting to and fro.

My code is based on the example, and now it works so thats fine for me. And to display it on an PCD8544 i need to convert it