| | |
int to float to float to int
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 36
Reputation:
Solved Threads: 0
i need help
I need to display
Example
23.4
and display 23 and .4
how do i do this?
so i need to convert a decimal into fraction
like this
http://www.cimt.plymouth.ac.uk/proje...7/bk7_17i1.htm
i need to display 4 and 0.276
I need to display
Example
23.4
and display 23 and .4
how do i do this?
so i need to convert a decimal into fraction
like this
http://www.cimt.plymouth.ac.uk/proje...7/bk7_17i1.htm
i need to display 4 and 0.276
Last edited by yasaswyg; Jul 27th, 2009 at 4:48 pm. Reason: found out
•
•
•
•
I need to display
Example
23.4
and display 23 and .4
c Syntax (Toggle Plain Text)
double d = 23.4; double w = (int)d; /* w == 23.0 */ double p = d - w; /* p == 0.4 */
Last edited by Tom Gunn; Jul 27th, 2009 at 5:01 pm.
-Tommy (For Great Justice!) Gunn
•
•
Join Date: Jul 2009
Posts: 36
Reputation:
Solved Threads: 0
•
•
•
•
One easy way is with the type system. When you cast a floating point value it cuts off the precision. If you subtract that result from the original, you get the precision:
c Syntax (Toggle Plain Text)
double d = 23.4; double w = (int)d; /* w == 23.0 */ double p = d - w; /* p == 0.4 */
THanks alot.
you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:
'd' is a double (floating point) variable containing 23.4.
'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.
'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'
now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)
.
'd' is a double (floating point) variable containing 23.4.
'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.
'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'
now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)
.
Last edited by jephthah; Jul 28th, 2009 at 12:32 am. Reason: .
•
•
Join Date: Jul 2009
Posts: 36
Reputation:
Solved Threads: 0
im sorry but i havent learned that in my class
•
•
•
•
you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:
'd' is a double (floating point) variable containing 23.4.
'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.
'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'
now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)
.
•
•
Join Date: Aug 2009
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
i need help
I need to display
Example
23.4
and display 23 and .4
how do i do this?
The problem I'm running into is coming up with a generic algorithm for storing the 0.4 part as just 4. I don't know how many digits the number will be every time, so I can't use a constant multiplier to move the decimal.
This is what I've come up with so far:
float n = 123.456; // 123 int nw = (int)n; // 456 int nf = (int)( ( n - nw ) * pow( 10, ??? ) );
I would much prefer a mathematical solution, but I would be willing to convert to a string and back, if that's the only way.
Floating type does not have fixed precision.
and if you enter
123.456
try to print that variable - you'll get something like that
123.455969706597869578956
So you have to convert its number into string and parse it.
Or choose precision and your code will be like that
But this method is not precise.
and if you enter
123.456
try to print that variable - you'll get something like that
123.455969706597869578956
So you have to convert its number into string and parse it.
Or choose precision and your code will be like that
cpp Syntax (Toggle Plain Text)
int prec = 5; int nf = (int)( ( n - nw ) * pow( 10, prec ) );
Last edited by Protuberance; Aug 17th, 2009 at 2:06 pm.
"If a problem can be decided - it is not needed to worry, and if to decide it is impossible - worrying is useless." (с)
![]() |
Similar Threads
- warning C4305: '=' : truncation from 'double' to 'float' (C)
- seperate float's decimal portion (C)
- Cannot convert parameter 1 from 'float *' 'to float' (C++)
- Selection Sort on a string with char and int? (C++)
- Float Help (C++)
- error C2664: cannot convert parameter 1 from 'float (void)' to 'float' (Game Development)
- int problem (C++)
- error C2664: cannot convert parameter 1 from 'float **__w64 ' to 'float *' (C)
Other Threads in the C Forum
- Previous Thread: modify the "save as" function to "save"
- Next Thread: seperate float's decimal portion
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






