CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

int to float

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kontakt



Joined: 27 Jan 2011
Posts: 7
Location: HU

View user's profile Send private message

int to float
PostPosted: Thu Jul 12, 2012 10:14 am     Reply with quote

How can I make a float number from to integer:

float val;
int val1=12, val2=3;

I would like to make: val=12.3
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Thu Jul 12, 2012 10:41 am     Reply with quote

Hi

Maybe this...

Code:
void mfloat(void){
float val;
int val1=12, val2=3;
val=val1+(float)val2/10;
printf("%3.1f\r\n",val);
}
kontakt



Joined: 27 Jan 2011
Posts: 7
Location: HU

View user's profile Send private message

PostPosted: Fri Jul 13, 2012 1:03 am     Reply with quote

Thank you, that was my first idea, but I can not believe that there is not better/nicer sloution Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19439

View user's profile Send private message

PostPosted: Fri Jul 13, 2012 1:15 am     Reply with quote

Well if you want a floating point result, there has to be floating point maths 'somewhere'.
Remember there is actually no need to go 'float' at all though (vastly quicker).
Code:

void dfloat(void){
   int16 val;
   int val1=12, val2=3;
   val=((int16)val1*10)+val2;
   printf("%4.1w\r\n",val);
}


Generates the integer 123, and then displays this as 12.3.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group