How to get integer part of a float ?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Flyingrock
Posts: 45
Joined: 12 Dec 2008 18:21

How to get integer part of a float ?

#1 Post by Flyingrock » 17 Feb 2017 04:44

Hi,

After calculation in my programm I get a float number eg: 363.08954.

I wish keep only 363, I don't want to convert into a string, but get an integer, this integer will be use again in my programm to determine a pixel position.

Any idea ?

Francis

diode_blade
Posts: 172
Joined: 24 Aug 2014 17:55
Location: Sheffield

Re: How to get integer part of a float ?

#2 Post by diode_blade » 17 Feb 2017 08:08

Look at the c math library there are functions in there that will give you both the fraction part as well as the Integer part or just do this

Code: Select all

volatile float kad=363.4567;
volatile unsigned int int_part;

int_part=(unsigned int)kad;

"When the bugs strike it's no good bashing your head against a brick wall, you might damage the wall"

MaGiK
Posts: 897
Joined: 19 Apr 2013 10:00

Re: How to get integer part of a float ?

#3 Post by MaGiK » 17 Feb 2017 10:08

Hello there FlyingRock (<--- Nice name :mrgreen: )

Why don't you just define the result variable as an integer instead of a float?

In case that doesn't work for you, then please follow the advice of (Diode Blade), a guy with a cool name like that couldn't possibly give a bad advice :lol:

Kindest Regards :D
My hobby is collecting MikroElektronika products.
Gotta catch them all!

Flyingrock
Posts: 45
Joined: 12 Dec 2008 18:21

Re: How to get integer part of a float ?

#4 Post by Flyingrock » 22 Feb 2017 18:14

Hi, diode_blade and MaGiK

Sorry to reply so late.

I didn't find function in math library to extract the integer part.
I tried what you have recommended and it's working.

Thank you for your help.

Post Reply

Return to “mikroC PRO for PIC General”