View previous topic :: View next topic |
Author |
Message |
kontakt
Joined: 27 Jan 2011 Posts: 7 Location: HU
|
int to float |
Posted: Thu Jul 12, 2012 10:14 am |
|
|
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
|
|
Posted: Thu Jul 12, 2012 10:41 am |
|
|
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
|
|
Posted: Fri Jul 13, 2012 1:03 am |
|
|
Thank you, that was my first idea, but I can not believe that there is not better/nicer sloution |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Fri Jul 13, 2012 1:15 am |
|
|
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 |
|
|
|